C# 使用 Min 或 Max 时如何处理 LINQ 中的空值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9852599/
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 handle nulls in LINQ when using Min or Max?
提问by Ignacio Soler Garcia
I have the following Linq query:
我有以下 Linq 查询:
result.Partials.Where(o => o.IsPositive).Min(o => o.Result)
I get an exception when result.Partials.Where(o => o.IsPositive) does not contains elements. Is there an elegant way to handle this other than splitting the operation in two and checking for null? I have a class full of operations like this one.
当 result.Partials.Where(o => o.IsPositive) 不包含元素时,我得到一个异常。除了将操作一分为二并检查是否为空之外,是否有一种优雅的方法来处理这个问题?我有一堂课充满了这样的操作。
EDIT: The question is related with LINQ to Objects.
编辑:问题与 LINQ to Objects 有关。
This is the Exception I'm getting (translated it says: The sequence is empty):
这是我得到的异常(翻译它说:序列为空):


采纳答案by Adrian Iftode
A short summary of the calculation of a Min
Min 计算的简短摘要
var min = result.Partials.Where(o => o.IsPositive).Min(o => o.Result);
This is your case: if there are no matching elements, then the Mincall will raise an exception (InvalidOperationException).
这是您的情况:如果没有匹配的元素,则Min调用将引发异常 ( InvalidOperationException)。
var min = result.Partials.Where(o => o.IsPositive)
.Select(o => o.Result)
.DefaultIfEmpty().Min();
DefaultIfEmptywill create an enumeration over the 0 element, when there are no elements in the list. How do you know that 0 is the Min or 0 stands for a list with no elements?
DefaultIfEmpty当列表中没有元素时,将在 0 元素上创建一个枚举。您怎么知道 0 是 Min 或 0 代表没有元素的列表?
var min = result.Partials.Where(o => o.IsPositive)
.Min(o => (decimal?)o.Result);
Here min is either null (defaul(decimal?)) or the actual Min found. So a consumer of this result will know that if the result is null then the list had no elements and when the result is a value of decimal, then the list had some elements and the Min of those elements is that value.
这里的 min 要么是 null ( defaul(decimal?)) 要么是找到的实际 Min。因此,此结果的使用者将知道,如果结果为空,则列表没有元素,而当结果是十进制值时,则列表有一些元素,这些元素的 Min 就是该值。
However, when this doesn't matter, then min.GetValueOrDefault(0)can be called.
但是,当这无关紧要时,则min.GetValueOrDefault(0)可以调用。
回答by Jon
You can't use Min(or Max) if the sequence is empty. If that shouldn't be happening, you have a different issue with how you define result. Otherwise, you should check if the sequence is empty and handle appropriately, eg:
如果序列为空,则不能使用Min(or Max)。如果这不应该发生,那么您对如何定义result. 否则,您应该检查序列是否为空并进行适当处理,例如:
var query = result.Partials.Where(o => o.IsPositve);
min = query.Any() ? query.Min(o => o.Result) : 0; // insert a different "default" value of your choice...
回答by Kendall Frey
You can use the DefaultIfEmptymethod to ensure the collection has at least 1 item:
您可以使用该DefaultIfEmpty方法来确保集合至少有 1 个项目:
result.Partials.Where(o => o.IsPositive).Select(o => o.Result).DefaultIfEmpty().Min();

