Show / Hide Table of Contents

    ISqlModelMapper.LoadAllByPageAsync<TModel>(int currentIndex, int pageSize, CancellationToken cancellationToken) Method

    .NET Standard 2.x

    Asynchronously retrieves data starting from a specified row according to the SQL query (without the Where clause) defined by the primary table in a TModel class.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Syntax

    public Task<ILoadable<TModel>> LoadAllByPageAsync<TModel>(int currentIndex, int pageSize, CancellationToken cancellationToken)
    

    Type Parameters

    TModel

    The type of a model class.

    Parameters

    currentIndex System.Int32

    The zero-based index number of the first row.

    pageSize System.Int32

    The number of rows per page.

    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.

    Remarks

    If the SQL query defined in TModel makes query from multiple tables, this method only retrieves and loads data from the primary table.

    Examples

    The following code example demonstrates how to asynchronously load the specified page according to the SQL query (without the Where clause) defined by the Department class. It uses a CancellationTokenSource type parameter to make this operation cancelable.

    using System;
    using System.Collections.Generic;
    using System.Threading;
    using System.Threading.Tasks;
    using Appeon.ApiDoc.Models.School;
    
    namespace Appeon.ApiDoc.ISqlModelMapperExamples
    {
        public class LoadAllByPageAsyncExample
        {
            private readonly SchoolContext _context;
            
            public LoadAllByPageAsyncExample(SchoolContext dataContext)
            {
                // Sets the data context.
                _context = dataContext;
            }
    
            public async Task<IList<Department>> Example2(CancellationTokenSource cts)
            {
                var mapper = _context.SqlModelMapper;
                
                IList<Department> deptsPage1 = null;
                
                try 
                {
                    // Asynchronously loads the first page only (2 records per page)
                    // If a task has been cancelled, the call to the ISqlModelMapper.LoadAllByPageAsync 
                    // method throws an OperationCanceledException.
                    deptsPage1 = (await mapper.LoadAllByPageAsync<Department>(0, 2, cts.Token)).ToList();
                    
                    Console.WriteLine("Departments (Page 1):");
                    foreach (var department in deptsPage1)
                    {
                        Console.WriteLine(department.Name);
                    }    
                }
                catch (OperationCanceledException e)
                {
                    // The operation was cancelled before completion.
                    Console.WriteLine(e.Message);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                finally
                {
                    cts.Dispose();
                }
                
                return deptsPage1;
                
                /* The code produces the following output:
                
                    A task was canceled.
                */
            }
        }
    }
    

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon