LinqToDB.LinqToDBException error in group by linq query in nopcommerce

199 views Asked by At

Here is my group by query

var query =  (
    from fee in _walletRepository.Table
    group fee by fee.PartyId into groupResult
    select new
    {
        partyId = groupResult.Select(x => x.PartyId),
        Balance = groupResult.Sum(f => f.Receipt > 0 ? f.Receipt : 0) - groupResult.Sum(f => f.Payment > 0 ? f.Payment : 0)
    }).OrderByDescending(x => x.Balance);

var tmp = await query.ToPagedListAsync(pageIndex, pageSize);

return new PagedList<OGSWallet>(tmp.Select(x => new OGSWallet
{
    PartyId = Convert.ToInt32(x.partyId),
    AvailableBalance = x.Balance
}).ToList(), tmp.PageIndex, tmp.PageSize, tmp.TotalCount);

Whenever I run the query, it throws error in query following line var tmp = await query.ToPagedListAsync(pageIndex, pageSize);

? Error : LinqToDB.LinqToDBException: You should explicitly specify selected fields for server-side GroupBy() call or add AsEnumerable() call before GroupBy() to perform client-side grouping.

Is there any idea is it query problem or any other problem?

1

There are 1 answers

0
Svyatoslav Danyliv On

Problem with line partyId = groupResult.Select(x => x.PartyId). PartyId is the key of the grouping and should be retrieved from groupResult.Key

var query =  (
    from fee in _walletRepository.Table
    group fee by fee.PartyId into groupResult
    select new
    {
        partyId = groupResult.Key,
        Balance = groupResult.Sum(f => f.Receipt > 0 ? f.Receipt : 0) - groupResult.Sum(f => f.Payment > 0 ? f.Payment : 0)
    }).OrderByDescending(x => x.Balance);