IEmbeddedLoadable.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<IEmbeddedLoadable<TModel>> IncludeAsync(Expression<Func<TModel, object>> property, bool cascade = false, CancellationToken cancellationToken = default);
Parameters
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 the 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<IEmbeddedLoadable<TModel>>
Returns a task that represents the asynchronous operation.
Remarks
The IEmbeddedLoadable.IncludeAsync
method can be used to load the data for the specified embedded properties rather than loading data for all properties. This can greatly improve the data retrieval efficiency.
Examples
The following code example demonstrates how to load departments asynchronously courses for a department.
using Appeon.ApiDoc.Models.School;
using SnapObjects.Data;
using System;
namespace Appeon.ApiDoc.IEmbeddedLoadableExamples
{
public class IncludeAsyncExample
{
private SchoolContext _context;
public IncludeAsyncExample(SchoolContext dataContext)
{
// Sets the data context.
_context = dataContext;
}
public async void Example()
{
var mapper = _context.SqlModelMapper;
// Loads the information for the Engineering department but
// it does not load courses here.
var dept = (await mapper.LoadAsync<DepartmentInfo>())
.ToList()
.Find(o => o.Name == "Engineering");
Console.WriteLine("Department: {0}",
dept.Name);
// Loads courses that belong to the Engineering department.
await mapper.LoadEmbedded(dept).IncludeAsync(o => o.Courses);
Console.WriteLine();
Console.WriteLine("Courses:");
foreach (CourseInfo course in dept.Courses)
{
Console.WriteLine(course.Title);
}
/*The code produces the following output:
Department: Engineering
Courses:
Chemistry
Physics
*/
}
}
}
Example Refer To
Model Classes: DepartmentInfo CourseInfo
Applies to
.NET Standard
2.x