Significant difference between Single, SingleOrDefault, First, FirstOrDefault

It is difficult for people new to LINQ to understand the differences between First, FirstOrDefault, Single, and SingleOrDefault.

There are many cases where you want to get a single result but the where clause is not part of the Primary key. This is possible by using Single, SingleOrDefault, First, and FirstOrDefault.

Single(), SingleOrDefault(), First(), and FirstOrDefault() are Enumerable class extension methods.

This blog will explain what to use and when to use it.

1. Single()

By using Single(), you actually state that you know just one item will be returned, hence LINQ returns exactly one thing rather than a collection containing only one item.

Exception Thrown:  The result contains 0 or more than 1 element.

var users = new List<User>
{
    new() {FirstName = "Geeta", LastName = "Desai", Age = 18},
    new() {FirstName = "Jeny", LastName = "Patel", Age = 21},
    new() {FirstName = "Jay", LastName = "Desai", Age = 23},
    new() {FirstName = "John", LastName = "Desai", Age = 36},
    new() {FirstName = "Mich", LastName = "Patel", Age = 45},
};

var result = users.Single(x => x.FirstName == "Geeta");

2. SingleOrDefault()

When zero or one result is expected in the input collection, the call SingleOrDefault() returns the one result if exactly one result is present, Default if no results are present, and exception if more than one result is present.

You may want to search for an element and check if it is the only one in the collection, instead of worrying about an exception if not found – the. The SingleOrDefault method is the best option! (Note: When the element provided by criteria is not found, it returns null.)

Exception Thrown: The result contains more than one element.

var users = new List<User> 
{ 
    new() {FirstName = "Geeta", LastName = "Desai", Age = 18}, 
    new() {FirstName = "Jeny", LastName = "Patel", Age = 21}, 
    new() {FirstName = "Jay", LastName = "Desai", Age = 23}, 
    new() {FirstName = "John", LastName = "Desai", Age = 36}, 
    new() {FirstName = "Mich", LastName = "Patel", Age = 45}, 
};

var result = users.SingleOrDefault(x => x.FirstName == "abc"); // returns null

var someoneWithDesaiLastName = users.SingleOrDefault(x => x.LastName == "Desai"); // throws an exception

3. First()

Using First() indicates that you want the first element of the sequence.

Exception Thrown: The result contains no elements.

var users = new List<User> 
{ 
    new() {FirstName = "Geeta", LastName = "Desai", Age = 18}, 
    new() {FirstName = "Jeny", LastName = "Patel", Age = 21}, 
    new() {FirstName = "Jay", LastName = "Desai", Age = 23}, 
    new() {FirstName = "John", LastName = "Desai", Age = 36}, 
    new() {FirstName = "Mich", LastName = "Patel", Age = 45}, 
};
var result = users.First(x => x.FirstName == "Jeny");

4. FirstOrDefault()

When there are zero or more results expected in the input collection, FirstOrDefault() returns the first item if there are multiple results, and Default if there are none.

You might want to get the first identical element and not worry about an exception if it’s not found – the. The FirstOrDefault method is the best option (note: it returns null if the element given by criteria is not found).

Exception Thrown: Only if the source is empty (they all do this)

var users = new List<User> 
{ 
    new() {FirstName = "Geeta", LastName = "Desai", Age = 18}, 
    new() {FirstName = "Jeny", LastName = "Patel", Age = 21}, 
    new() {FirstName = "Jay", LastName = "Desai", Age = 23}, 
    new() {FirstName = "John", LastName = "Desai", Age = 36}, 
    new() {FirstName = "Mich", LastName = "Patel", Age = 45}, 
};
var result = users.FirstOrDefault(x => x.FirstName == "abc"); // returns null

var someoneWithPatelLastName = users.FirstOrDefault(x => x.LastName == "Patel"); // returns first item from the list, in this example it would be JenyPatel, Age 21

Performance

Single() and SingleOrDefault() are slower than First() and FirstOrDefault() because Single() evaluates a whole collection of elements but First() finds only the first match.

Is SingleOrDefault or FirstOrDefault faster?

Because FirstOrDefault passes the collection until it finds the first match, it is generally faster than SingleOrDefault. SingleOrDefault runs through the whole collection in search of a single match.

What is the purpose of selecting single or SingleOrDefault first or FirstOrDefault?

Use Single or SingleOrDefault to throw an exception if the result set consists of a large number of records. Use SingleOrDefault to return a default value if the result set contains no records. Use First or FirstOrDefault when you always want one record irrespective of what the result set contains.

–>If no result is returned by Single() or First(), an exception is thrown; SingleOrDefault() and FirstOrDefault() catch the exception and return null or default (ResultDataType).

Submit a Comment

Your email address will not be published.

Subscribe

Select Categories