Reputation: 4332
I am using a generic repository to retrieve objects from the database and while lazy loading is not available in EntityFramework Core, sometimes I would need to select related objects.
The repo code looks like this:
public T GetSingle(Expression<Func<T, bool>> predicate, params Expression<Func<T, object>>[] includeProperties)
{
IQueryable<T> query = _context.Set<T>();
foreach (var includeProperty in includeProperties)
{
query = query.Include(includeProperty);
}
return query.Where(predicate).FirstOrDefault();
}
and telling it to select a related object is simple:
.GetSingle(id,x=>x.ObjectviaFK)`
I can't wrap my head around building an expression that will allow selecting nested objects (similar to .Include(...).ThenInclude(...)
from context), something like .GetSingle(id, x=>Parent,parent=>Grand,grand=>GrandGrand)
. How do I do this?
Upvotes: 0
Views: 579
Reputation: 26936
It would seem if you don't want to change your GetSingle
you could just put in the correct lambdas to include the nested objects?
`.GetSingle(id, x=>x.Parent, x=>x.Parent.Grand, x=>x.Parent.GrandGrand)
Upvotes: 1