Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ Any() and Single() vs. SingleOrDefault() with null check [closed]

Tags:

c#

linq

In what cases is each solution preferred over the other?

Example 1:

if (personList.Any(x => x.Name == "Fox Mulder"))
{
  this.Person = personList.Single(x => x.Name == "Fox Mulder");
}

Example 2:

var mulder = personList.SingleOrDefault(x => x.Name == "Fox Mulder");

if (mulder != null)
{
  this.Person = mulder;
}
like image 653
user1172282 Avatar asked Aug 06 '15 18:08

user1172282


People also ask

What is difference between single and SingleOrDefault in Linq?

The SingleOrDefault() method does the same thing as Single() method. The only difference is that it returns default value of the data type of a collection if a collection is empty, includes more than one element or finds no element or more than one element for the specified condition.

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

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. When you want a default value if the result set contains no record, use FirstOrDefault.

What the difference between single or single or default?

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 SingleOrDefault in Linq?

SingleOrDefault<TSource>(IEnumerable<TSource>, TSource)Returns the only element of a sequence, or a specified default value if the sequence is empty; this method throws an exception if there is more than one element in the sequence.


1 Answers

Both Single and SingleOrDefault will enumerate the collection beyond the first matching result to verify that there is exactly one element matching the criteria, stopping at either the next match or the end of the collection. The first example will be slightly slower, since the Any call will enumerate enough of the collection (possibly all of it) to determine whether any elements meet the criteria, stopping at either the first match or the end of the collection.

There is one other critical difference: the first example could throw an exception. Single will return the matching element if there is exactly one, and throw an exception otherwise. Checking with Any does not verify this; it only verifies that there is at least one.

Based one these two reasons (primarily/especially the second reason), the SingleOrDefault approach is preferable here.


So, there are three cases here.

Case 1: No items match the condition

Option 1: .Any enumerates the entire set and returns false; .Single never executes.

Option 2: .SingleOrDefault enumerates the entire set and returns null.

Options essentially equivalent.

Case 2: Exactly one item matches the condition

Option 1: Any enumerates enough of the set to find the single match (could be the first item, could be the entire set). Next, Single enumerates the entire set to find that one item and confirm that no others match the condition.

Option 2: SingleOrDefault enumerates the entire set, returns the only match.

In this case, option 2 is better (exactly one iteration, compared to (1, 2] iterations)

Case 3: More than one element matches the condition

Option 1: Any enumerates enough to find the first match. Single enumerates enough to find the second match, throws exception.

Option 2: SingleOrDefault enumerates enough to find the second match, throws exception.

Both throw exceptions, but option 2 gets there more quickly.

like image 52
yoozer8 Avatar answered Sep 23 '22 05:09

yoozer8