如果有可能找不到该元素,我应该使用 Single() 还是 SingleOrDefault() ?
你最想看到什么?
try
{
var item = list.Single(x => x.HasFoo);
}
catch(InvalidOperationException e)
{
throw new InvalidOperationException("Exactly one item with foo expected, none found", e);
}
或者:
var item = list.SingleOrDefault(x => x.HasFoo);
if (item == null)
throw new InvalidOperationException("Exactly one item with foo expected, none found");
这里的最佳实践是什么?哪一个使异常更容易理解?
What would you prefer to see?
try
{
var item = list.Single(x => x.HasFoo);
}
catch(InvalidOperationException e)
{
throw new InvalidOperationException("Exactly one item with foo expected, none found", e);
}
Or:
var item = list.SingleOrDefault(x => x.HasFoo);
if (item == null)
throw new InvalidOperationException("Exactly one item with foo expected, none found");
What's the best practice here? Which one makes the exception more comprehensible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
SingleOrDefault()
Single()
另请记住,有一个可能情况的数量:
并且:
并且不要忘记
First()
、FirstOrDefault()
和Any()
SingleOrDefault()
if 0 or 1 items are expectedSingle()
if 1, not 0 or 2 and more, item is expectedAlso keep in mind that there are a number of possible scenarios:
And:
And don't forget about
First()
,FirstOrDefault()
andAny()
我会写:
如果不返回单个项目的情况如此常见,您需要更友好的错误消息,那么这真的是一个例外吗?
I would write:
If the case where this does not return a single item is so common you need a friendlier error message, then is it really an exception at all?
我认为这样写是可以的,
也可以这样写。
但如果您实际上不需要访问该值,
I think it's OK to write
but you can also write
if you don't actually need access to the value.
实际上,它们是相同的。但我更喜欢第二个,因为在前两个中抛出了一个异常。 异常的代价是昂贵的。
Practically, they are the same. But I prefer second one since one exception is thrown while in the first two. Exceptions are expensive.
如果您总是期望列表中的一个元素,只需使用
并在顶级方法中捕获异常,您将在其中记录异常的详细信息并向用户显示友好的消息。
如果您有时期望 0 个或超过 1 个元素,最安全的方法将是
我假设的,验证是否存在超过 1 条记录并不重要。
代码项目文章LINQ:Single vs. SingleOrDefault
If you ALWAYS EXPECT one element in the list, just use
and catch exception at the top level method, where you will log details of exception and show friendly message to the user.
If you sometimes expect 0 or more than 1 elements, the safest method will be
I assumed, that it is not important to verify, are more than 1 record exist or not.
Similar question was discussed in Code Project article LINQ: Single vs. SingleOrDefault
假设您询问的是 0..1 场景,我更喜欢 SingleOrDefault,因为它可以让您指定自己的方式来处理“未找到”场景。
因此,使用一点语法糖的一个好方法是:
其中 notFound() 是:
Assuming you were asking about the 0..1 scenario, I prefer SingleOrDefault because it lets you specify your own way to handle the "nothing found" scenario.
So, a good way to do using a little syntactic sugar, would be:
where notFound() is:
我宁愿在获取元素之前检查列表中的元素数量,而不是等待异常,然后抛出一个新异常。
这些建议很紧凑,但在我看来,更好的是更明确。
(此外,在您的建议中,例外情况并不严格有效:当可能有多个例外时,他们会说“未找到”)
编辑:Jeebus,添加了一行来首先过滤迂腐人士的列表。 (我认为这对任何人来说都是显而易见的)
I'd rather see a check to the number of elements in the list before getting the element, rather than waiting for an exception, then throwing a new one.
It's nice that the suggestions are compact, but better to be more explicit IMO.
(Also in your suggestions the exceptions are not strictly valid: they say 'none found' when there could be more than one)
Edit: Jeebus, added one line to filter the list first for pedantic people. (I thought it would have been obvious for anyone)
我同意 Kieren Johnstone 的观点,不要等待异常,这是相当昂贵的,当然当你多次调用这个方法时。
您的第一个代码片段甚至更昂贵,因为您等待原始异常,而不是给自己抛出一个新异常。
I agree with Kieren Johnstone, don't wait for the exception this is pretty costly, sure when you call this method alot of times.
You're first code snippet is even more expensive, because you wait for the original exception, and than throw yourself a new one.
单身
单一或默认
这是示例:-
Single
SingleOrDefault
here is sample example:-