(C# ASP.NET Core LINQ) SingleOrDefault, Find, FirstOrDefault, Single and First

This is an explanation of SingleOrDefault, FirstOrDefault and Find methods from a practical point of view. We have explained when to use which one and how to use. We have also explained how Single is different from First, and how Single is different from SingleOrDefault, and also how First is different from FirstOrDefault.

Categories | About |     |  

Parveen,

Our working set

Let us suppose our model is

public class DoctorProfile
{

  [Key]
  [DatabaseGenerated(DatabaseGeneratedOption.None)]
  public int ID { get; set; }

  // name of a doctor 
  public String Name { get; set; }

  // fees charged by a doctor 
  public int Fees { get; set; }
}

In the following discussion we have assumed that

  1. that we have already configured our database
  2. that our DbContext instance is _ctx
  3. that our DbSet<DoctorProfile> is called Doctors

Video Explanation

Please watch the following youtube video:

When and how to use Find()?

If you want to find a record by its primary key, then the easiest way is to use the Find() method.

The following code searches for a doctor with ID = 1 because ID has been specified as the primary key on the model class.


var doctor = _ctx.Doctors.Find(1);

// null if not found 
if (null == doctor)
{

}
else
{
    // found 
}

When and how to use FirstOrDefault()?

If you want to find a record by an indexed property, or by any criterion that is based on any property then the best way is to use FirstOrDefault() method.


// returns the first doctor with fees exceeding 300 
// the ordering is the default ordering 
// we should have used OrderBy to specify the ordering 

var doctor = _ctx.Doctors
                  .FirstOrDefault(q => q.Fees > 300);

// returns null if not found 

When and how to use SingleOrDefault()?

SingleOrDefault is used if the table is expected to contain exactly one matching record.

This method returns null if no record is found.

This method throws an exception if more than one records found. So it can be a useful tool for confirming that the table contains exactly one matching record.


DoctorProfile doctor = null;

try
{
  doctor = _ctx.Doctors
                    .SingleOrDefault(q => q.Fees == 300);

  if (null == doctor)
  {
    // none found 

  }

}
catch (Exception)
{
  // more than 1 found 
}

Single vs SingleOrDefault and First vs FirstOrDefault?

SingleOrDefault returns null if record not found, but Single throws an exception if record not found. So we recommend using SingleOrDefault, and use an if condition for null testing.

FirstOrDefault returns null if record not found, but First throws an exception if record not found. So we recommend using FirstOrDefault, and use an if condition for null testing.


This Blog Post/Article "(C# ASP.NET Core LINQ) SingleOrDefault, Find, FirstOrDefault, Single and First" by Parveen is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Comments and Discussion