Show / Hide Table of Contents

    ILoadable.Include(int index, Expression<Func<TModel, object>> property, bool cascade = false) Method

    .NET Standard 2.x

    Loads data for the specified embedded property in TModel for the specified row.

    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

      ILoadable<TModel> Include(int index, Expression<Func<TModel, object>> property, bool cascade = false);
    

    Parameters

    index System.Int32

    The zero-based index for the row in the result set.

    property System.Linq.Expressions.Expression

    An embedded property of TModel class.

    This property is specified by an expression.

    cascade System.Boolean

    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 granddetail model. See master-detail-granddetail for more info.

    The default value is false.

    Returns

    SnapObjects.Data.ILoadable<TModel>

    Returns the current ILoadable<TModel> object whose methods can be used to further obtain the data result set or perform data operations such as embedded queries.

    Remarks

    The ILoadable.Include method can be used to load the data for the specified embedded property for the specified row, rather than loading data for all of the properties. This can greatly improve the data retrieval efficiency.

    Examples

    The following code example demonstrates how to load data for the specified row.

    using Appeon.ApiDoc.Models.School;
    using System;
    
    namespace Appeon.ApiDoc.ILoadableExamples
    {
        public class IncludeExample
        {
            private SchoolContext _context;
    
            public IncludeExample(SchoolContext dataContext)
            {
                // Sets the data context.
                _context = dataContext;
            }
    
            public void Example2()
            {
                var mapper = _context.SqlModelMapper;
    
                // Loads the information of all departments.
                var loadable = mapper.Load<DepartmentInfo>();
    
                // Gets the index of the English department.
                int index = loadable.ToList().FindIndex(o => o.Name == "English");
    
                // Loads courses for the English department only.
                // Retrieves students who have selected the course and online flags. 
                var depts = loadable.Include(index, o => o.Courses, cascade: true)
                                    .ToList();
    
                Console.WriteLine("Courses of {0} Deparment:",
                    depts[index].Name);
    
                foreach (CourseInfo course in depts[index].Courses)
                {
                    Console.WriteLine();
                    Console.WriteLine("Course Name: {0}", course.Title);
                    Console.WriteLine("Student IDs:");
                    foreach (CourseStudentInfo student in course.Students)
                    {
                        Console.WriteLine(student.StudentID);
                    }
                }
    
                /* The code produces the following output:
    
                Courses of English Deparment:
    
                Course Name: Composition
                Student IDs:
                2
                3
                6
                7
                8
    
                Course Name: Poetry
                Student IDs:
                2
                3
    
                Course Name: Literature
                Student IDs:
                6
                7
                8
                */
            }
        }
    }
    

    Example Refer To

    Model Classes: DepartmentInfo CourseInfo CourseStudentInfo

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon