Home > Back-end >  Error Translating Linq query whileselecting from group on Generic repository
Error Translating Linq query whileselecting from group on Generic repository

Time:08-18

I have the following method that is

public async Task<IList<TGroupSelect>> GetAll<TEntity,TGroup,TGroupSelect>(Func<IQueryable<T>, IIncludableQueryable<T, object>> include = null,
           Expression<Func<T, bool>> whereClause = null, Expression<Func<T, TEntity>> selector = null,

          Func<IQueryable<TEntity>, IQueryable<IGrouping<object, TEntity>>> groupBy = null,
          Func<IGrouping<object, TEntity>, TGroupSelect> groupSelector = null)
        {
            var result = _ctx.Set<T>().AsQueryable();

            result = include(result);

            var res = (whereClause is null ? result.Select(selector) : result.Where(whereClause).Select(selector));

           

            var group = groupBy(res);

            return  group.Select(groupSelector).ToList();
        }

on the last select it throws the following error

The LINQ expression 'DbSet<UserMeetings>()
    .Include(x => x.User)
    .Include(x => x.Meeting)
    .ThenInclude(x => x.Host)
    .Where(x => x.UserId == __UserId_0)
    .Select(x => x)
    .GroupBy(y => y.Meeting)' 

could not be translated. Either rewrite the query in a form that can
 be translated, or switch to client evaluation explicitly by inserting
 a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or
 'ToListAsync'. 
See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.

so how can I make it work after the group by sentence without calling AsAsyncEnumerable?

here is how I used it


 var meetings =await  repo.GetAll<UserMeetings,Meeting, IGrouping<Meeting, UserMeetings>> (
                include:x=>x.Include(x=>x.User).Include(x=>x.Meeting).ThenInclude(x=>x.Host),
                whereClause: filter,
                selector: x=>x,
                groupBy: x => { return x.GroupBy(x => x.Meeting); }, 
                groupSelector:(x)=> x);

CodePudding user response:

Accourding to Microsoft documentation in .NET 5, GroupBy isn't translated to SQL query and you should use AsEnumerable or ToList before GroupBy statement.

Your code should be like this:

public async Task<IList<TGroupSelect>> GetAll<TEntity,TGroup,TGroupSelect>(Func<IQueryable<T>, IIncludableQueryable<T, object>> include = null,
          Expression<Func<T, bool>> whereClause = null, Expression<Func<T, TEntity>> selector = null,
          Func<IQueryable<TEntity>, IQueryable<IGrouping<object, TEntity>>> groupBy = null,
          Func<IGrouping<object, TEntity>, TGroupSelect> groupSelector = null)
{
    var result = _ctx.Set<T>().AsQueryable();

    result = include(result);

    var res = await (whereClause is null 
               ? result.Select(selector) 
               : result.Where(whereClause).Select(selector)
             ).ToListAsync();

    var group = groupBy(res);

    return  group.Select(groupSelector).ToList();
}
  • Related