Entity Framework Generic repository including properties through parameter

后端 未结 3 1771
日久生厌
日久生厌 2020-12-20 06:59

I have an implementation of a Generic Repository in Entity Framework which I am trying to improve to use the .Include(..) function provided by EF instead of including the na

3条回答
  •  一整个雨季
    2020-12-20 07:20

    You can use the params Expression>[] includeProperties instead of string parameter

    public IQueryable GetAll(
        Expression> filter = null,
        Func, IOrderedQueryable> orderBy = null,
        params Expression>[] includeProperties)
    {
        IQueryable query = dbSet;
    
            if (filter != null)
            {
                query = query.Where(filter);
            }
    
            foreach (var includeProperty in includeProperties)
            {
                query = query.Include(includeProperty);
            }
    
            if (orderBy != null)
            {
                return orderBy(query);
            }
            else
            {
                return query;
            }
    }
    

提交回复
热议问题