IEmbeddedLoadable.Include(Expression<Func<TModel, object>> property, bool cascade = false) Method
.NET Standard 2.x
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
IEmbeddedLoadable<TModel> Include(Expression<Func<TModel, object>> property, bool cascade = false);
Parameters
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 the granddetail model. See master-detail-granddetail for more info.
The default value is false
.
Returns
SnapObjects.Data.IEmbeddedLoadable<TModel>
Returns the current IEmbeddedLoadable<TModel>
object whose methods can be used to further obtain the data result set or perform data operations such as embedded queries.
Remarks
The IEmbeddedLoadable.Include
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 courses for a department.
using Appeon.ApiDoc.Models.School;
using System;
namespace Appeon.ApiDoc.IEmbeddedLoadableExamples
{
public class IncludeExample
{
private SchoolContext _context;
public IncludeExample(SchoolContext dataContext)
{
// Sets the data context.
_context = dataContext;
}
public void Example()
{
var mapper = _context.SqlModelMapper;
// Loads the information for the Engineering department but
// it does not load courses here.
var dept = mapper.Load<DepartmentInfo>()
.ToList()
.Find(o => o.Name == "Engineering");
Console.WriteLine("Department: {0}",
dept.Name);
// Loads courses that belong to the Engineering department.
mapper.LoadEmbedded(dept).Include(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