Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use Single() or SingleOrDefault() if there is a chance that the element won't be 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?

like image 638
the_drow Avatar asked Apr 11 '11 09:04

the_drow


People also ask

When should the SingleOrDefault () method be used?

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.

What is the difference between single or SingleOrDefault?

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.

What is the difference between FirstOrDefault () and SingleOrDefault () extension method in Linq?

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.

Which is faster SingleOrDefault or FirstOrDefault?

c# - FirstOrDefault is signicantly faster than SingleOrDefault while viewing ANTS profiler - Stack Overflow. Stack Overflow for Teams – Start collaborating and sharing organizational knowledge.

When to use single or singleordefault instead of firstordefault?

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.

When to use single or singleordefault in EF Core?

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.

What is the use of singleordefault in IEnumerable?

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.

What is singleordefault in Salesforce?

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.


2 Answers

  • Use SingleOrDefault() if 0 or 1 items are expected
  • Use Single() if 1, not 0 or 2 and more, item is expected

Also keep in mind that there are a number of possible scenarios:

  • You got 0 when 0 or 1 was expected (ok)
  • You got 1 when 0 or 1 was expected (ok)
  • You got 2 or more when 0 or 1 was expected (error)

And:

  • You got 0 when 1 was expected (error)
  • You got 1 when 1 was expected (ok)
  • You got 2 or more when 1 was expected (error)

And don't forget about First(), FirstOrDefault() and Any()

like image 51
abatishchev Avatar answered Sep 19 '22 15:09

abatishchev


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?

like image 40
Myster Avatar answered Sep 23 '22 15:09

Myster