How to make dynamic inclusion of navigation properties?

后端 未结 1 1752
佛祖请我去吃肉
佛祖请我去吃肉 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<TEntity>, IQueryable<TEntity>>.

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

    public IQueryable<TEntity> GetBySpecification(ISpecification<TEntity> spec = null, bool tracking = true, params Func<IQueryable<TEntity>, IQueryable<TEntity>>[] includes)
    {
        var query = _context.Set<TEntity>().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<User>, IQueryable<User>>[]
        {
            (q) => q.Include(u => u.Roles).ThenInclude(r => r.Permissions),
        });
    
    0 讨论(0)
提交回复
热议问题