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
wit
.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.
First()
FirstOrDefault()
We have an UserInfos table, which have some records as shown below. On the basis of this table below I have created example...
How to use First()
var result = dc.UserInfos.First(x => x.ID == 1);
There is only one record where ID== 1. Should return this record
ID: 1 First Name: Manish Last Name: Dubey Email: xyz@xyz.com
var result = dc.UserInfos.First(x => x.FName == "Rahul");
There are multiple records where FName == "Rahul". First record should be return.
ID: 7 First Name: Rahul Last Name: Sharma Email: xyz1@xyz.com
var result = dc.UserInfos.First(x => x.ID ==13);
There is no record with ID== 13. An error should be occur.
InvalidOperationException: Sequence contains no elements
How to Use FirstOrDefault()
var result = dc.UserInfos.FirstOrDefault(x => x.ID == 1);
There is only one record where ID== 1. Should return this record
ID: 1 First Name: Manish Last Name: Dubey Email: xyz@xyz.com
var result = dc.UserInfos.FirstOrDefault(x => x.FName == "Rahul");
There are multiple records where FName == "Rahul". First record should be return.
ID: 7 First Name: Rahul Last Name: Sharma Email: xyz1@xyz.com
var result = dc.UserInfos.FirstOrDefault(x => x.ID ==13);
There is no record with ID== 13. The return value is null
Hope it will help you to understand when to use First()
or FirstOrDefault()
.
First of all, Take
is a completely different method. It returns an IEnumerable<T>
and not a single T
, so that's out.
Between First
and FirstOrDefault
, you should use First
when you're sure that an element exists and if it doesn't, then there's an error.
By the way, if your sequence contains default(T)
elements (e.g. null
) and you need to distinguish between being empty and the first element being null
, you can't use FirstOrDefault
.
First:
FirstOrDefault:
From: http://www.technicaloverload.com/linq-single-vs-singleordefault-vs-first-vs-firstordefault/
.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.
Others have very well described the difference between First()
and FirstOrDefault()
. I want to take a further step in interpreting the semantics of these methods. In my opinion FirstOrDefault
is being overused a lot. In the majority of the cases when you’re filtering data you would either expect to get back a collection of elements matching the logical condition or a single unique element by its unique identifier – such as a user, book, post etc... That’s why we can even get as far as saying that FirstOrDefault()
is a code smell not because there is something wrong with it but because it’s being used way too often. This blog post explores the topic in details. IMO most of the times SingleOrDefault()
is a much better alternative so watch out for this mistake and make sure you use the most appropriate method that clearly represents your contract and expectations.