LINQ Fundamentals - First() Vs FirstOrDefault()

There are four famous concepts in LINQ, almost every C# developer knows about them and has used them in their daily development work.

I am talking about First and FirstOrDefault, Single and SingleOrDefault; these are all extension methods.

For this example, we created Employee class, declared some properties and defined a static method in Employee class.

Following is the Employee class:

  1. class Employee {  
  2.     public int Id {  
  3.         get;  
  4.         set;  
  5.     }  
  6.     public string Name {  
  7.         get;  
  8.         set;  
  9.     }  
  10. }  

Following is the array of Employee and it returns IEnumerable of Employee; i.e., IEnumerable<Employee>. Here is the hard-coded data, but you can get data from the database.

  1. public static IEnumerable < Employee > GetAllEmp() {  
  2.     IEnumerable < Employee > developers = new Employee[] {  
  3.         new Employee {  
  4.             Id = 101, Name = "Ashutosh"  
  5.         },  
  6.         new Employee {  
  7.             Id = 102, Name = "Nilesh"  
  8.         },  
  9.         new Employee {  
  10.             Id = 103, Name = "Amar"  
  11.         },  
  12.         new Employee {  
  13.             Id = 104, Name = "Ashutosh"  
  14.         },  
  15.         new Employee {  
  16.             Id = 105, Name = "Ashutosh"  
  17.         }  
  18.     };  
  19.     return developers;  
  20. }  

Let's start with First() and FirstOrDefault().

First()

It returns the first element from a sequence. It means when we query in a collection on specific criteria, if multiple elements are found in a collection of given criteria then the first element of a sequence will return.

  1. var emp = Employee.GetAllEmp().Where(x => x.Name == "Ashutosh").First();  
  2. Console.WriteLine($ "Id:{emp.Id},Name:{emp.Name}");  

In the above LINQ query, we are filtering employee name from the collection of Employees and if the filter criteria is matched, we get the below output on the console:

LINQ

As you can see, there are three records in a sequence which match the input criteria but First() returns only the first element from the sequence.

In the above case we did positive testing, now we need to do negative testing with an invalid Employee Name.

Here, we are taking an employee which does not exist in our employee collection, let’s suppose it's  Rahul.

  1. var emp = Employee.GetAllEmp().Where(x => x.Name == "Rahul").First();  
  2. Console.WriteLine($ "Id:{emp.Id},Name:{emp.Name}"); 

 

When we run the console application we get an exception indicating the sequence contains no element.

LINQ

So, if there is no record in the collection or database table which matches the input criteria, then First() method will throw an exception, because it can't handle the null value.

FirstOrDefault()

FirstOrDefault works same as First() does, FirstOrDefault returns the first element from a sequence, but here there is an advantage over First(), so if there is no record in the collection which matches input criteria then FirstOrDefault() can handle null values and it does not throw an exception.

Conclusion

Use First() when you are sure that a query must return a record, and use FirstOrDefault() when you are not sure whether it will return a record or not.

In the second part we will discuss Single() and SingleOrDefault()


Similar Articles