First vs Single


In LINQ, First and Single are methods used to retrieve elements from a sequence, but they behave differently in terms of handling multiple elements and the conditions under which they throw exceptions. Here’s a comparison of First and Single:

First

Use First when you are interested in the first occurrence of an element (or the first element if no condition) and don’t mind if there are multiple matches.

Example

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        var numbers = new List<int> { 1, 2, 3, 4, 5 };

        // Get the first number (no predicate)
        var firstNumberone = numbers.First();
        Console.WriteLine("FIrst = "+firstNumberone.ToString());

        // Get the first number greater than 2
        var firstNumberGreaterThanTwo = numbers.First(n => n > 2);
        Console.WriteLine("Second = "+firstNumberGreaterThanTwo.ToString());

        // Throws InvalidOperationException if no element is found.
        var firstNumberfour = numbers.First(n => n ==6); 
        Console.WriteLine("Three = "+firstNumberfour.ToString());
    }
}

Output

LINQ

Single

Use Single when you expect exactly one matching element and want to ensure that no more than one match exists.

Example

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        var singlenumbers = new List<int> { 2 };

        //Gets the single element, throws if there’s not exactly one.
        var firstNumbersingle = singlenumbers.Single();
        Console.WriteLine("FIrst = " + firstNumbersingle.ToString());

        var numbers = new List<int> { 1, 2, 3, 4, 5 };

        //  //Gets the single element, throws if there’s not exactly one.
        var firstNumberfour = numbers.Single(n => n == 5);
        Console.WriteLine("Third = " + firstNumberfour.ToString());

        //Gets the single element, throws if there’s not exactly one.
        var firstNumberone = numbers.Single();
        Console.WriteLine("FIrst = " + firstNumberone.ToString());  // Error

        //Gets the single element, throws if there’s not exactly one.
        var firstNumberGreaterThanTwo = numbers.Single(n => n > 2); 
        Console.WriteLine("Second = " + firstNumberGreaterThanTwo.ToString()); //Error
    }
}

Output

LINQ

Difference Between First and Single in LINQ

First Single
Retrieves the first element of a sequence that matches a specified condition. Retrieves a single element from a sequence that matches a specified condition.
The first element that matches the condition, or the first element if no condition is specified. The only element that matches the condition.
Does not require the sequence to have exactly one element; can handle multiple matching elements. Requires the sequence to have exactly one element; throws an exception if there are zero or more than one matching elements.
Throws InvalidOperationException if no element is found when using First without a predicate. Throws InvalidOperationException if no element is found.
Does not throw an exception if more than one element matches the condition. Throws InvalidOperationException if more than one element matches the condition.
var firstNumber = numbers.First(); (Gets the first element.) var singleNumber = numbers.Single(); (Gets the single element, throws if there’s not exactly one.)
var firstEven = numbers.First(n => n % 2 == 0); (Gets the first even number.) var singleEven = numbers.Single(n => n % 2 == 0); (Gets the only even number, throws if there are none or more than one.)

Prev Next