通过C#将Lambda分组到通用存储库EF

时间:2016-05-28 10:06:27

标签: c# entity-framework generics lambda

我有一个查询存储库库,这里我有一个通用的实现来返回&没有被分页,现在我需要在系统的这种通用死亡中实现分组。

返回特定类型的 PagedResult 对象:

public class PagedResult<T> : PagedData
{
    public IList<T> Results { get; set; }
}

PagedData 对象只有一些随机返回字段...

有问题的厄运方法如下:

public virtual PagedResult<TEntity> GetPageGrouped(int pageIndex, int pageSize, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy, 
        Expression<IGrouping<object,TEntity>> groupBy, Expression<Func<TEntity, bool>> filter = null, params Expression<Func<TEntity, object>>[] navs)
    {
        IQueryable<TEntity> query = this.Context.Set<TEntity>();

        if (filter != null)
        {
            query = query.Where(filter);
        }

        foreach (var nav in navs)
        {
            query = query.Include(nav);
        }

        return new PagedResult<TEntity>
        {
            Results = groupBy(query).orderBy(query).Skip((pageIndex) * pageSize).Take(pageSize).ToList(),
            RowCount = query.Count(),
            CurrentPage = pageIndex
        };
    }

要使用它,我想称之为:

var list = _repo.GetPage(page, pageSize, t => t.OrderBy(p => p.ClientId), t => t.GroupBy(z => new { z.Field1, z.Field2, z.Field3 }));

我的问题可能是我不理解 IGrouping ,因为我无法获得正在构建的查询,而我正在死亡。

任何帮助都会很棒!

修改 如果我用正常的linq表达式来编写它,它看起来类似于:

return context.Table1s.GroupBy(i => i.Policy)
                  .Select(g => g.First())
                  .Orderby(i => i.Policy)
                  .Skip(endingRecord).Take(page)
                  .ToList();

2 个答案:

答案 0 :(得分:1)

你可以尝试

Expression<IGrouping<object,TEntity>>[] groupBy
  

这样可以将多个参数发送到您的Expression

存储库
public async Task<IEnumerable<T>> GetPagedAsync(
       Func<IQueryable<T>,
       IOrderedQueryable<T>> orderBy,
       Expression<Func<T, bool>> filter = null,
       int? page = 0,
       int? pageSize = null, params Expression<Func<T, object>>[] includes,
       Expression<IGrouping<object,TEntity>>[] groupBy)
    {
        IQueryable<T> query = dbSet;



        if (filter != null)
        {
            query = query.Where(filter);
        }

        //Includes
        if (includes != null && includes.Any())
        {
            query = includes.Aggregate(query,
                      (current, include) => current.Include(include));
        }

        if (orderBy != null)
            query = orderBy(query);
        else
            throw new ArgumentNullException("The order by is necessary in Pagining");




        if (page != null && page > 0)
        {
            //(0-1)
            if (pageSize == null) throw new ArgumentException("The take paremeter supplied is null, It should be included when skip is used");
            query = query.Skip(((int)page - 1) * (int)pageSize);
        }

        if (pageSize != null)
        {
            query = query.Take((int)pageSize);
        }

        return await query.ToListAsync();
    }

我像这样使用它

.MyRepository
.GetPagedAsync(h => h.OrderBy(l => l.Rating)
 ,g => g.GroupId == _categoryId, skip, take);

答案 1 :(得分:0)

您没有在“ GetPagedAsync”中使用lambda

Expression<IGrouping<object,TEntity>>[] groupBy 

Example:  groupBy(query )

那是真的吗?