0

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();
BeardedNoob
  • 3
  • 1
  • 6

2 Answers2

0

You could try

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

This will allows to send multiple parameter to your repository my 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();
    }

I use it like this

.MyRepository
.GetPagedAsync(h => h.OrderBy(l => l.Rating)
 ,g => g.GroupId == _categoryId, skip, take);
Eldho
  • 7,795
  • 5
  • 40
  • 77
0

You did not use lambda in "GetPagedAsync"

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

Example:  groupBy(query )

Well, is that true?