Show / Hide Table of Contents

    ILoadable.IncludeAsync(Expression<Func<TModel, object>> property, bool cascade = false, CancellationToken cancellationToken = default) Method

    .NET Standard 2.x

    Asynchronously loads the specified embedded property in TModel for all rows.

    If the ModelEmbedded attribute is applied in the property, the property will use the SQL query (defined in the detail model class specified by the ModelType property of the ModelEmbedded attribute) to retrieve data from database. The cascade parameter can be used to decide whether to retrieve data for the embedded properties in the detail model or the granddetail model.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Syntax

    Task<ILoadable<TModel>> IncludeAsync(Expression<Func<TModel, object>> property, bool cascade = false, CancellationToken cancellationToken = default);
    

    Parameters

    property System.Linq.Expressions.Expression<Func<TModel, object>>

    An embedded property of TModel class.

    This property is specified by an expression.

    cascade System.Boolean

    (Optional) Only when the ModelEmbedded attribute is applied in the property, the cascade parameter can be used to decide whether to retrieve data for the embedded properties in the detail model or the granddetail model. See master-detail-granddetail for more info.

    The default value is false.

    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 load all of the departments and the courses for each department asynchronously.

    using Appeon.ApiDoc.Models.School;
    using System;
    
    namespace Appeon.ApiDoc.ILoadableExamples
    {
        public class IncludeAsyncExample
        {
            private SchoolContext _context;
            
            public IncludeAsyncExample(SchoolContext dataContext)
            {
                // Sets the data context.
                _context = dataContext;
            }
    
            public async void Example1()
            {
                var mapper = _context.SqlModelMapper;
                
                // Asynchronous loads the information of all departments and loads courses for 
                // each department.
                var depts = (await (await mapper.LoadAsync<DepartmentInfo>())
                                    .IncludeAsync(o => o.Courses))
                                    .ToList();
                                    
                Console.WriteLine("The first department: {0}",
                    depts[0].Name);
                Console.WriteLine("Courses:");
                foreach (CourseInfo course in depts[0].Courses)
                {
                    Console.WriteLine(course.Title);
                }
                
                Console.WriteLine();
                Console.WriteLine("The second department: {0}",
                    depts[1].Name);
                Console.WriteLine("Courses:");
                foreach (CourseInfo course in depts[1].Courses)
                {
                    Console.WriteLine(course.Title);
                }
                
                /* The code produces the following output:
                
                    The first department: Engineering
                    Courses:
                    Chemistry
                    Physics
        
                    The second department: English
                    Courses:
                    Composition
                    Poetry
                    Literature
                */
            }
        }
    }
    

    Example Refer To

    Model Classes: DepartmentInfo CourseInfo CourseStudentInfo

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon