Show / Hide Table of Contents

    ISqlBuilderLoader.LoadByPageAsync(int currentIndex, int pageSize, object[] parameters, CancellationToken cancellationToken) Method

    .NET Standard 2.x

    Asynchronously retrieves data starting from a specified row by the built SQL statement.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Syntax

    public Task<ILoadable<TModel>> LoadByPageAsync(int currentIndex, int pageSize, object[] parameters, CancellationToken cancellationToken)
    

    Parameters

    currentIndex System.Int32

    The zero-based index number of the first row.

    pageSize System.Int32

    The number of rows per page.

    parameters System.Object[]

    One or more values that you want to use as retrieval arguments in the SQL SELECT statement.

    cancellationToken CancellationToken

    A cancellation token that can be used by other objects or threads to receive notice of cancellation.

    Returns

    Task<ILoadable<TModel>>

    Returns a task that represents the asynchronous operation.

    Examples

    The following code example demonstrates how to use the LoadByPageAsync method. It uses a CancellationTokenSource type parameter to make this operation cancelable.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading;
    using System.Threading.Tasks;
    using Appeon.ApiDoc.Models.School;
    using SnapObjects.Data;
    
    namespace Appeon.ApiDoc.ISqlBuilderLoaderExamples
    {
        public class LoadByPageAsyncExample
        {
            private readonly SchoolContext _context;
            private ISqlModelMapper mapper;
            
            public LoadByPageAsyncExample(SchoolContext dataContext)
            {
                // Sets the data context.
                _context = dataContext;
                mapper = _context.SqlModelMapper;
            }
    
            public async Task<int> Example2()
            {
                CancellationTokenSource cts = new CancellationTokenSource();
                var builder = mapper.GetQueryBuilder<Person_Student>();
                
                int count = 0;
                
                try
                {
                    // If a task has been cancelled, the call to the
                    // ISqlBuilderLoader.LoadByPageAsync method throws an
                    // OperationCanceledException.
                    count = (await builder.LoadByPageAsync(0, 30,
                                                            new object[] { "Student" },
                                                            cts.Token)).ToList().Count;
                                          
                    Console.WriteLine("Count: {0}", count);
                }
                catch (OperationCanceledException e)
                {
                    // The operation was cancelled before completion.
                    Console.WriteLine(e.Message);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                finally
                {
                    cts.Dispose();
                }
                
                return count;
                
                /*The code produces the following output:
                 
                    A task was canceled.
                */
            }
        }
    }
    

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon