这是在 linqpad 中运行的 linq,我没有收到任何错误,但是当我运行我们的项目时,我收到此错误(不支持嵌套查询。Operation1='GroupBy' Operation2='MultiStreamNest'),您可以给出溶液
var ledgerReport = (from i in db.Items
join si in db.SalesInvoiceDetails on i.ItemCode equals si.ItemCode
join cat in db.categories on i.catCode equals cat.catCode
join locw in
(from loc in db.LocationStocks
group loc by new { loc.ItemCode } into g1
select new { Qty1 = (Decimal?)g1.Sum(x => x.Qty) ?? 0, g1.Key.ItemCode }) on i.ItemCode equals locw.ItemCode into we
where si.DispatchDate >= startdate && si.DispatchDate <= enddate
group si by new
{
si.ItemCode,
i.Description,
Qtt = (Decimal?)we.Sum(x => x.Qty1) ?? 0,
i.LowestLevel
} into g
select new
{
g.Key.ItemCode,
g.Key.Description,
qoh = g.Key.Qtt,
quantity = (Decimal?)g.Sum(x => x.QtyOrdered) ?? 0,
lowlevel = g.Key.LowestLevel
}).tolist();
这是一个非常老的问题,但有一些赞成票但没有答案,谷歌把我发送到这里。我花了很长时间,但终于找到了解决方案,并认为如果其他人绝望的话,分享一下会很好。 :)
尝试将任意 orderby 放入 Linq 中。在这个例子中,我认为它看起来像:
group si by new
{
si.ItemCode,
i.Description,
Qtt = (Decimal?)we.Sum(x => x.Qty1) ?? 0,
i.LowestLevel
} into g
orderby g.ItemCode
select new
{
g.Key.ItemCode,
g.Key.Description,
qoh = g.Key.Qtt,
quantity = (Decimal?)g.Sum(x => x.QtyOrdered) ?? 0,
lowlevel = g.Key.LowestLevel
}).tolist();
也可以在 db.LocationStocks 嵌套查询中尝试它,但对我来说,它是将 orderby 放入顶层来做到这一点。
我最好的猜测是 orderby 触发了嵌套查询的一些不同处理。还有另一个想法“以不同的方式重写连接”,但这对我不起作用。我在中找到这个解决方案的帖子也让我认为在相关表之间添加外键可以做到这一点,但我没有尝试这样做。