LINQ First

Summary: in this tutorial, you’ll learn how to use the LINQ First() method to find the first element in a sequence that satisfies a condition.

Introduction to the LINQ First() method

The First() is a LINQ extension method that returns the first element in a sequence that satisfies a condition.

Here’s the syntax of the First() method:

public static TSource First<TSource>(
    this IEnumerable<TSource> source
);Code language: C# (cs)

In this syntax:

  • TSource is the type of elements in the source sequence.
  • source is the input sequence with the type IEnumerable<T>.

The method returns the first element of the source sequence with the type TSource.

If the source is null, the method throws an ArgumentNullException. Also, if the source is empty, the method throws an InvalidOperationException.

To specify a condition, you use an overload of the First() method like this:

public static TSource First<TSource>(
    this IEnumerable<TSource> source,
    Func<TSource,bool> predicate
);Code language: C# (cs)

The predicate is a function that tests each element for a condition.

The method throws an InvalidOperationException if it couldn’t find any element that satisfies the condition.

In general, the method throws an InvalidOperationException if the source is empty or no element satisfies a condition. To avoid the exception, you can use the FirstOrDefault() method.

The FirstOrDefault() method returns the first element of a sequence. If it could not find any element that satisfied a condition, it returns a default value.

LINQ First() method examples

Let’s take some examples of using the LINQ First() method.

1) Using LINQ First() method to with a specified condition

The following example demonstrates how to use the First() method to find the first element number in a list of numbers:

using static System.Console;

List<int> numbers = new() { 1, 3, 7, 2, 8, 6 };

int firstEvenNumber = numbers.First(n => n % 2 == 0);

WriteLine($"The first even number is: {firstEvenNumber}");Code language: C# (cs)

In this example, the First() method returns/ the first even number because of the condition in the lambda expression:

n => n % 2 == 0Code language: C# (cs)

2) Using LINQ FirstOrDefault() method to handle exceptions

If no element in a sequence satisfies a condition, the First() method throws an exception.

To avoid it, you can use the FirstOrDefault() method instead. For example:

using static System.Console;

List<int> numbers = new (){ 1, 3, 5  };

int firstEvenNumber = numbers.FirstOrDefault(n => n % 2 == 0);

WriteLine($"The first even number is: {firstEvenNumber}");Code language: C# (cs)

Output:

The first even number is: 0Code language: C# (cs)

In this example, the numbers list has only odd numbers. Therefore, the FirstOrDefault() method returns a default value of an integer, which is zero.

Summary

  • Use LINQ First() method to find the first element of a sequence that satisfies a specified condition.
Was this tutorial helpful ?