Linq First vs Single

Tuesday, February 19, 2019

By: Chris Dunn

What's the difference between LINQs Single method and First method? While they can both return the same result, they serve a different purpose depending on the underlying sequence of data. Using the wrong method can cause problems in your code now, and as your data grows.

First()

First() should be used on sequences of data with a count of one or more. This method is returning the "FIRST" of possibly "MANY". If there are no results an exception will be thrown. One option, in this case, is to use the sister method FirstOrDefault() which returns the default value (usually null) when there is a sequence of zero.

For the sample code below we will be using the following array of strings containing names.

	//sample data
	string[] names = { "Bob", "Gary", "Dave", "Barry", "Geena", "Harry" };
		

The following is examples show various scenarios using the First() method.

	
	
	var first = names.First();
	//value will be Bob
	Console.WriteLine(first);
	
	first = names.Where(m => m.StartsWith("B")).First();
	//value will still be bob
	Console.WriteLine(first);
	
	try
	{
		//throw an exception since count will be 0
		first = names.Where(m => m.StartsWith("A")).First();
		Console.WriteLine(first);
	}
	catch (InvalidOperationException ex)
	{
		//InvalidOperationException 
		Console.WriteLine("No result.");
	}
	
	first = names.Where(m => m.StartsWith("A")).FirstOrDefault();
	//value will be null
	Console.WriteLine(first!=null?first:"No result.");
	

Single()

Single() should be used when you expect only one result. If the sequence contains more than one value, an exception will be thrown, as it should. Also, if the sequence contains zero values, an exception will also be thrown. A similar sister method to First(), SingleOrDefault() exists to handle this case.

The following examples show various scenarios with the Single() method.

	//LINQ single examples
	string single;

	try
	{ 
		single = names.Single();
		//throw an exception since there is more than one
		Console.WriteLine(single);
	}
	catch(InvalidOperationException ex )
	{
		//InvalidOperationException 
		Console.WriteLine("No result.");
	}
	
	single = names.Where(m => m.StartsWith("D")).Single();
	//success since one a single result is returned.
	Console.WriteLine(single);
	
	try
	{
		single = names.Where(m => m.StartsWith("A")).Single();
		//throw an exception since count will be 0
		Console.WriteLine(single);
	}
	catch (InvalidOperationException ex)
	{
		//InvalidOperationException 
		Console.WriteLine("No result.");
	}
	
	single = names.Where(m => m.StartsWith("A")).SingleOrDefault();
	//value will be null
	Console.WriteLine(single != null ? single : "No result.");
	

Hopefully that gives you a good idea of the output for different scenarios for both First() and Single() LINQ methods. Make sure you understand the anticipated result sets for each query you make.  This will help you decide which method best serves your code.

Tags: c# linq

Copyright 2019 Cidean, LLC. All rights reserved.

Proudly running Umbraco 7. This site is responsive with the help of Foundation 5.