Is there a way to "group up" the .Include
calls below & encapsulate them in another method, which I can call like below?
var entity = _db.Entity.AsNoTracking()
.AsSplitQuery()
.Include(x => x.Prop1)
.Include(x => x.Prop2)
.Include(x => x.Prop3)
.Include(x => x.Prop4)
.FirstOrDefault(x => x.Id == id);
Something like:
var entity = _db.Entity.AsNoTracking()
.AllIncludes()
.FirstOrDefault(x => x.Id == id);
I know about extension methods on IEnumerable but I couldn't find what I need.
CodePudding user response:
You can achieve this by writing an extension method, which will allow you to "add" an AllIncludes
method on top of the database entity.
This will allow you to encapsulate the .Include
calls.
Assuming _db.Entity
is of type IQueryable<Entity>
:
public static class QueryableExtensions
{
public static IQueryable<Entity> AllIncludes(this IQueryable<Entity> source)
{
return source
.Include(x => x.Prop1)
.Include(x => x.Prop2)
.Include(x => x.Prop3)
.Include(x => x.Prop4);
}
}
Call, as expected:
var entity = _db.Entity.AsNoTracking()
.AllIncludes()
.FirstOrDefault(x => x.Id == id);