C# 如何在源集合为空时强制 LINQ Sum() 返回 0
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17593371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to force LINQ Sum() to return 0 while source collection is empty
提问by John Mayer
Basically when I do the following query, if no leads were matched the following query throws an exception. In that case I'd prefer to have the sum equalize 0 rather than an exception being thrown.
Would this be possible in the query itself - I mean rather than storing the query and checking query.Any()?
基本上,当我执行以下查询时,如果没有匹配的线索,则以下查询会引发异常。在这种情况下,我希望总和等于 0,而不是抛出异常。这在查询本身中是否可行 - 我的意思是而不是存储查询和检查query.Any()?
double earnings = db.Leads.Where(l => l.Date.Day == date.Day
&& l.Date.Month == date.Month
&& l.Date.Year == date.Year
&& l.Property.Type == ProtectedPropertyType.Password
&& l.Property.PropertyId == PropertyId).Sum(l => l.Amount);
采纳答案by Simon Belanger
Try changing your query to this:
尝试将您的查询更改为:
db.Leads.Where(l => l.Date.Day == date.Day
&& l.Date.Month == date.Month
&& l.Date.Year == date.Year
&& l.Property.Type == ProtectedPropertyType.Password
&& l.Property.PropertyId == PropertyId)
.Select(l => l.Amount)
.DefaultIfEmpty(0)
.Sum();
This way, your query will only select the Amountfield. If the collection is empty, it will return one element with the value of 0and then the sum will be applied.
这样,您的查询将只选择该Amount字段。如果集合为空,它将返回一个值为 的元素,0然后应用总和。
回答by tukaef
I prefer to use another hack:
我更喜欢使用另一个黑客:
double earnings = db.Leads.Where(l => l.Date.Day == date.Day
&& l.Date.Month == date.Month
&& l.Date.Year == date.Year
&& l.Property.Type == ProtectedPropertyType.Password
&& l.Property.PropertyId == PropertyId)
.Sum(l => (double?) l.Amount) ?? 0;
回答by Kovács Róbert
Try this instead, it's shorter:
试试这个,它更短:
db.Leads.Where(..).Aggregate(0, (i, lead) => i + lead.Amount);
回答by Mona
db.Leads.Where(l => l.Date.Day == date.Day
&& l.Date.Month == date.Month
&& l.Date.Year == date.Year
&& l.Property.Type == ProtectedPropertyType.Password
&& l.Property.PropertyId == PropertyId)
.Select(l => l.Amount)
.ToList()
.Sum();
回答by Pedro Ramos
That's win for me:
这对我来说是胜利:
int Total = 0;
Total = (int)Db.Logins.Where(L => L.id == item.MyId).Sum(L => (int?)L.NumberOfLogins ?? 0);
In my LOGIN table, in field NUMBEROFLOGINS some values are NULL and others have an INT number. I sum here total NUMBEROFLOGINS of all users of one Corporation (Each Id).
在我的 LOGIN 表中,在字段 NUMBEROFLOGINS 中,某些值为 NULL,而其他值为 INT 编号。我在这里总结了一个公司(每个 ID)的所有用户的总 NUMBEROFLOGINS。
回答by Maxim Lukoshko
Try:
尝试:
double earnings = db.Leads.Where(l => l.ShouldBeIncluded).Sum(l => (double?)l.Amount) ?? 0;
双倍收益 = db.Leads.Where(l => l.ShouldBeIncluded).Sum(l => (double?)l.Amount) ?? 0;
The query "SELECT SUM([Amount])" will return NULL for empty list. But if you use LINQ it expects that the "Sum(l => l.Amount)" returns double and it doesn't allow you to use "??" operator to set 0 for empty collection.
查询“ SELECT SUM([Amount])”将为空列表返回NULL。但是,如果您使用 LINQ,则它期望“ Sum(l => l.Amount)”返回双精度并且不允许您使用“ ??”运算符为空集合设置 0。
In order to avoid this situation you need to make LINQ expect "double?". You can do it by casting "(double?)l.Amount".
为了避免这种情况,您需要让 LINQ 期望“ double?”。您可以通过铸造“ (double?)l.Amount”来实现。
It doesn't affect the query to SQL but it makes LINQ working for empty collections.
它不会影响对 SQL 的查询,但它使 LINQ 可以处理空集合。

