nopcommerce 中按 linq 查询分组时出现 LinqToDB.LinqToDBException 错误

问题描述 投票:0回答:1

这是我的查询分组

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);

每当我运行查询时,它都会在以下行的查询中抛出错误

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

?错误:LinqToDB.LinqToDBException:您应该为服务器端 GroupBy() 调用显式指定选定字段,或在 GroupBy() 之前添加 AsEnumerable() 调用以执行客户端分组。

不知道是查询问题还是其他问题?

asp.net-mvc linq group-by nopcommerce linq2db
1个回答
0
投票

线路

partyId = groupResult.Select(x => x.PartyId)
有问题。
PartyId
是分组的关键,应从
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);
© www.soinside.com 2019 - 2024. All rights reserved.