Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use .First and when to use .FirstOrDefault with LINQ?

Tags:

c#

.net

linq

I've searched around and haven't really found a clear answer as to when you'd want to use .First and when you'd want to use .FirstOrDefault with LINQ.

  • When would you want to use .First? Only when you'd want to catch the exception if no results where returned?

    var result = List.Where(x => x == "foo").First();
    
  • And when would you want to use .FirstOrDefault? When you'd always want the default type if no result?

    var result = List.Where(x => x == "foo").FirstOrDefault();
    
  • And for that matter, what about Take?

    var result = List.Where(x => x == "foo").Take(1);
    
like image 419
Metro Smurf Avatar asked Jun 21 '09 19:06

Metro Smurf


People also ask

What is the difference between first () and FirstOrDefault () selector methods in Linq?

The major difference between First and FirstOrDefault is that First() will throw an exception if there is no result data for the supplied criteria whereas FirstOrDefault() will return the default value (null) if there is no result data.

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

SingleOrDefault() – 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.

What is difference between single and SingleOrDefault in Linq?

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.


3 Answers

I would use First() when I know or expect the sequence to have at least one element. In other words, when it is an exceptional occurrence that the sequence is empty.

Use FirstOrDefault() when you know that you will need to check whether there was an element or not. In other words, when it is legal for the sequence to be empty. You should not rely on exception handling for the check. (It is bad practice and might hurt performance).

Finally, the difference between First() and Take(1) is that First() returns the element itself, while Take(1) returns a sequence of elements that contains exactly one element.

like image 51
driis Avatar answered Oct 17 '22 23:10

driis


.First will throw an exception when there are no results. .FirstOrDefault won't, it will simply return either null (reference types) or the default value of the value type. (e.g like 0 for an int.) The question here is not when you want the default type, but more: Are you willing to handle an exception or handle a default value? Since exceptions should be exceptional, FirstOrDefault is preferred when you're not sure if you're going to get results out of your query. When logically the data should be there, exception handling can be considered.

Skip() and Take() are normally used when setting up paging in results. (Like showing the first 10 results, and the next 10 on the next page, etc.)

Hope this helps.

like image 42
Jeroen Landheer Avatar answered Oct 18 '22 00:10

Jeroen Landheer


.First() will throw an exception if there's no row to be returned, while .FirstOrDefault() will return the default value (NULL for all reference types) instead.

So if you're prepared and willing to handle a possible exception, .First() is fine. If you prefer to check the return value for != null anyway, then .FirstOrDefault() is your better choice.

But I guess it's a bit of a personal preference, too. Use whichever makes more sense to you and fits your coding style better.

like image 134
marc_s Avatar answered Oct 18 '22 00:10

marc_s