我有三张桌子:
A中的每条记录都可以是:
我开始将表B中的部分分组,如下所示:
SELECT
A.IdRecord, A.Qty, sum(isnull(B.Qty,0)) AS Expr1
FROM
RecordEntered AS A
LEFT OUTER JOIN
SoldInCountry AS B ON A.IdRecord = B.IdRecord
group by A.IdRecord, A.Qty
但我不知道该怎么做。
我想查询一下,向我展示我还有多少件库存。 像这样:
A.Qty - (SUM(ISNULL(B.Qty, 0)) + SUM(ISNULL(C.Qty, 0)))
我在SQL中编写了一个示例,但目标是LINQ:
from a in _ctx.....
where .....
select...
感谢
答案 0 :(得分:1)
在LINQ中进行完全外部联接并不容易(请参阅我的答案:https://stackoverflow.com/a/43669055/2557128)但你不需要这样做来解决这个问题:
var numInStock = from item in RecordEntered
select new {
item.Code,
Qty = item.Qty - (from sic in SoldInCountry where sic.IdRecord == item.IdRecord select sic.Qty).SingleOrDefault() -
(from soc in SoldOutCountry where soc.IdRecord == item.IdRecord select soc.Qty).SingleOrDefault()
};
我假设某个项目的每种类型只会有一个已售出的记录,如果可能有多个,则需要Sum
匹配的记录:
var numInStock = from item in RecordEntered
select new {
item.Code,
Qty = item.Qty - (from sic in SoldInCountry where sic.IdRecord == item.IdRecord select sic.Qty).DefaultIfEmpty().Sum() -
(from soc in SoldOutCountry where soc.IdRecord == item.IdRecord select soc.Qty).DefaultIfEmpty().Sum()
};