I have a query Repository base, and here I have a generic implementation to return records that are & aren't paged, and now I need to implement grouping in this generic death of a system.
A PagedResult object is returned of a specific type:
public class PagedResult<T> : PagedData
{
public IList<T> Results { get; set; }
}
PagedData object just has some random return fields...
The method of doom in question is the following:
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
};
}
And to use it, I want to call it like:
var list = _repo.GetPage(page, pageSize, t => t.OrderBy(p => p.ClientId), t => t.GroupBy(z => new { z.Field1, z.Field2, z.Field3 }));
My problem is maybe that I am not understanding IGrouping, as I cannot get this query that's being built up to work and I am dying.
Any assistance will be awesome!
EDIT If I were to write this with a normal linq expression it would look similar to:
return context.Table1s.GroupBy(i => i.Policy)
.Select(g => g.First())
.Orderby(i => i.Policy)
.Skip(endingRecord).Take(page)
.ToList();