Wednesday, August 14, 2013

LINQ- Difference between First and FirstOrDefault- First vs FirstOrDefault

In my previous post Single vs SingleOrDefault, we talk about Single and SingleOrDefault method and difference between these methods. In this post we are going to explore two more extension methods First and FirstOrDefault.


You can find some other useful topics and posts here. Below is a chart explaining about First() and FirstOrDefault(). You can easily differentiate these methods from this chart. I have also given examples of each method.



Description

When thrown Exception

When to Use

First()

Returns first element of a sequence.

There is no element in the result or source is null.

If more than one element is expected and you want only first element.

FirstOrDefault()

Returns first element of a sequence,  or a default value if no element is found.

Only if the source is null

If more than one element is expected and you want only first element. Also good if result is empty.

Examples

We have an UserInfo table, which have some records as shown below.
First and FirstorDefault in LINQ
We will use thee above table to querying for First() and FirstOrDefault(). Here I have used LINQ to SQL.

Single()


DataClasses1DataContext dc = new DataClasses1DataContext();

Statement

Expected Result

Actual Result

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: 10 First Name: Rahul Last Name: Kumar Email: xyz123@abc.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

FirstOrDefault()

DataClasses1DataContext dc = new DataClasses1DataContext();

Statement

Expected Result

Actual Result

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: 10 First Name: Rahul Last Name: Kumar Email: xyz123@abc.com

var result = dc.UserInfos.FirstOrDefault(x => x.ID ==13);

There is no record with ID== 13. An error should be occur.

null

Note- To use First() and FirstOrDefault(), consider the order of record to get the required result. For example, query
var result = dc.UserInfos.FirstOrDefault(x => x.FName == "Rahul");
will give you different result in compare to below query.
var result = dc.UserInfos.OrderBy(x => x. LName).FirstOrDefault(x => x.FName == "Rahul");

I hope this will be helpful for you. I would like to have any feedback from you. Your valuable feedback, question, or comments about this article are always welcome

No comments:

Post a Comment

If You Enjoyed This Post Please Take 5 Seconds To Share It.

^ Scroll to Top hgpromo