Linq to Sql,使用 lambda sum 作为 where 条件
我有以下查询
from p in _context.Products
where p.Purchases.Sum(item => item.CCAmount) > 0 && p.Purchases.Sum(item => item.CCAmount) > p.PayOuts.Sum((item => item.AmountPaid)
select p;
基本上我正在尝试检索所有购买金额总和大于 0 且购买金额总和大于我们已支付金额的产品(我们代表其他人销售产品并向他们付款)全额或部分付款)。问题是,如果支付表中没有特定产品的条目,则该产品不会出现在结果列表中。但是,如果我将付款插入付款表中,则该产品将出现在产品列表中。它几乎就像在一个空集合上使用 sum 不会像人们所期望的那样求值 ie 为 0。我在这里遗漏了一些东西吗?
感谢您的帮助。
I have the following query
from p in _context.Products
where p.Purchases.Sum(item => item.CCAmount) > 0 && p.Purchases.Sum(item => item.CCAmount) > p.PayOuts.Sum((item => item.AmountPaid)
select p;
Basically I am trying to retrieve all products that have a summed purchase amount greater than 0 and whose summed purchase amount is greater than the amount we have paid out (we are selling products on behalf of other people and pay them either in full or part payments). The problem is that if there are no entries in the payouts table for a particular product then that product does not appear in the resultant list. However if I insert a payout into the payouts table then that product will appear in the product list. Its almost as if using sum on an empty collection will not evaluate as one would expect i.e. as 0. Am I missing something here?
Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于,如果没有要求和的记录,SQL 中的
SUM
将返回null
。你需要作弊一点。
尝试:
或者以稍微不同的方式编写
The problem is that
SUM
in SQL returnsnull
if there are no records to sum.You need to cheat a little bit.
Try:
or written in a little bit different way