How to make dynamic inclusion of navigation properties?

后端 未结 1 1765
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-16 19:45

I\'ve a little problem. Assuming a Entity like this

public class FirstEntity
{
    public int ID { get; set; }
    public string Prop1 { get; set; }
    publ         


        
1条回答
  •  时光取名叫无心
    2021-01-16 20:24

    Parameterize with a Func, IQueryable>.

    And instead of .Select(r => r) you can simply use .AsQueryable().

    public IQueryable GetBySpecification(ISpecification spec = null, bool tracking = true, params Func, IQueryable>[] includes)
    {
        var query = _context.Set().AsQueryable();
        if (!tracking)
            query = query.AsNoTracking();
        if (includes != null)
            foreach (var include in includes)
                query = include(query);
        if (spec != null)
            query = query.Where(spec.Expression);
        return query;
    }
    
    return GetBySpecification(
        includes: new Func, IQueryable>[]
        {
            (q) => q.Include(u => u.Roles).ThenInclude(r => r.Permissions),
        });
    

    0 讨论(0)
提交回复
热议问题