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?
When you want an exception to be thrown if the result set contains many records, use Single or SingleOrDefault. When you want a default value is returned if the result set contains no record, use SingleOrDefault. When you always want one record no matter what the result set contains, use First or FirstOrDefault.
Single : It returns a single specific element from a collection of elements if element match found. An exception is thrown, if none or more than one match found for that element in the collection. SingleOrDefault: It returns a single specific element from a collection of elements if element match found.
SingleOrDefault() Vs. FirstOrDefault() in LINQ QuerySingleOrDefault() – Same as Single(), but it can handle the null value. First() - There is at least one result, an exception is thrown if no result is returned. FirstOrDefault() - Same as First(), but not thrown any exception or return null when there is no result.
c# - FirstOrDefault is signicantly faster than SingleOrDefault while viewing ANTS profiler - Stack Overflow. Stack Overflow for Teams – Start collaborating and sharing organizational knowledge.
A default value is returned, if no match is found for that element in the collection. You should take care of following points while choosing Single, SingleOrDefault, First and FirstOrDefault: When you want an exception to be thrown if the result set contains many records, use Single or SingleOrDefault.
It also contains the script of the database We use Single or SingleOrDefault in EF Core, when we expect only a single row to exist in the table. If the query returns more than one record, then the system will throw an exception.
SingleOrDefault (IEnumerable, Func): This method returns the only element of the collection or sequence which specifies the given condition and will throw an exception if more than one element exists that specifies the given condition.
SingleOrDefault. It returns a single specific element from a collection of elements if element match found. An exception is thrown, if more than one match found for that element in the collection. A default value is returned, if no match is found for that element in the collection.
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()
and Any()
I would write:
var item = list.Single(x => x.HasFoo);
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With