ILoadable.IncludeAsync(int index, 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 a 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
Task<ILoadable<TModel>> IncludeAsync(int index, Expression<Func<TModel, object>> property, bool cascade = false, CancellationToken cancellationToken = default);
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
(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 granddetail model. See master-detail-granddetail for more info.
The default value is false
.
cancellationToken
CancellationToken
(Optional) 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 data for the specified row 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 Example2()
{
var mapper = _context.SqlModelMapper;
// Asynchronous loads the information of all departments.
var loadable = await mapper.LoadAsync<DepartmentInfo>();
// Gets the index of the English department.
int index = loadable.ToList().FindIndex(o => o.Name == "English");
// Asynchronous loads courses for the English department only.
// Retrieves students who have selected the course and online flags.
var depts = (await loadable.IncludeAsync(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