我已经研究了一段时间,所以我想我会在这里发帖,看看是否有人知道如何将以下查询转换为LINQ。
这是MySQL查询:
SELECT SUM(line_ord.itemqty) AS LineOrderQTY, SUM(assemblyNumber.qty) AS
AssemblyQTY FROM line_ord
LEFT JOIN
(
SELECT sum(assemblyno.qty) AS qty, assemblyno.row_id FROM assemblyno
INNER JOIN line_ord ON assemblyno.row_id = line_ord.row_id
WHERE line_ord.bdnum = 'S61460'
) AS assemblyNumber ON line_ord.row_id = assemblyNumber.row_id
WHERE line_ord.bdnum = 'S61460'
这是我目前为LINQ查询所做的,但它没有返回正确的结果。
var items = (from c in Context.OrderLineItemData
join e in Context.AssemblyLabelData on c.ID equals e.RowID
where c.BreakdownNumber == breakdownNumber
group c by c.BreakdownNumber into g
select new
{
AssemblyQuantity= g.Sum(x => x.Quantity),
LineOrdQuantity = g.Sum(**WHAT GOES HERE?**)
}).FirstOrDefault()
我确实设法让它像这样工作,但对我来说似乎有点混乱。
var items = (from c in Context.OrderLineItemData
join e in Context.AssemblyLabelData on c.ID equals e.RowID
where c.BreakdownNumber == breakdownNumber
group c by c into g
select new
{
AssemblyQuantity= g.Sum(x => x.Quantity),
LineOrdQuantity = (from e in Context.OrderLineItemData where e.BreakdownNumber == breakdownNumber select e.Quantity).Sum()
}).FirstOrDefault();
有更好的方法吗?
答案 0 :(得分:0)
得到了我的回答。似乎必须创建一个查询对象,然后我可以用它来预先形成我的计算。
var items = (from od in Context.OrderLineItemData
join ad in Context.AssemblyLabelData on od.ID equals ad.RowID into odGroup
from g in odGroup.DefaultIfEmpty()
where od.BreakdownNumber == breakdownNumber
group g by new
{
breakdown = od.BreakdownNumber,
LineOrd = od,
AssemblyQty = g == null ? 0 : g.Quantity
}
into groupped
select new
{
Breakdown = groupped.Key.breakdown,
AssemblyQty = groupped.Sum(x => x.Quantity),
lineOrdQty = groupped.Key.LineOrd.Quantity
}
);
int remainingQuantity = items.Sum(x => x.lineOrdQty) - items.Sum(x => x.AssemblyQty);