Which is More Efficient: First or Single in LINQ?

Choosing the Right Method for Your Needs

BCAL
.Net Programming
4 min readSep 1, 2024

--

First or Single

Language Integrated Query (LINQ) is a powerful feature in .NET that makes it easy to query and manipulate data collections. Among the various methods in LINQ, First() and Single() are commonly used to find a single element. However, if you don’t understand the differences between these two methods, you might encounter issues later on. Let’s compare these methods to see which one is more efficient.

First() Method

First() method retrieves the first element in a sequence. As the name suggests, it returns the very first value that exists. However, if the sequence is empty, it throws an exception. In cases where the sequence might be empty, you can use the FirstOrDefault() method instead.

  • First(): Returns the first element in a sequence and throws an exception if the sequence is empty or if no elements match the specified condition.
  • FirstOrDefault(): Returns the first element in a sequence or a default value (0 for numeric types, null for reference types) if the sequence is empty or no elements match the condition.

Single() Method

The Single() method is used when you expect the sequence to contain exactly one element. If there is not exactly one element (i.e., if the sequence is empty or contains more than one element), it throws an exception. If you want to handle the case where the sequence might be empty, you can use the SingleOrDefault() method. However, even SingleOrDefault() will throw an exception if more than one element is present.

  • Single(): Returns the single element in a sequence if exactly one element exists. If the sequence is empty or contains more than one element, it throws an exception.
  • SingleOrDefault(): Returns the single element in a sequence or a default value if the sequence is empty. However, if more than one element exists, it throws an exception.

Performance Differences

Though First() and Single() might seem similar, they function differently, especially when it comes to performance. Below is an example using a benchmark library to compare the performance of First() and Single() in a sequence with 10,000 elements.

public IEnumerable<int> range;

[GlobalSetup]
public void Setup()
{
range = Enumerable.Range(0, 10000);
}

[Benchmark]
public void First_FirstElement()
{
var first = range.First(x => x == 0);
}

[Benchmark]
public void First_LastElement()
{
var first = range.First(x => x == 9999);
}

[Benchmark]
public void Single_FirstElement()
{
var single = range.Single(x => x == 0);
}

[Benchmark]
public void Single_LastElement()
{
var single = range.Single(x => x == 9999);
}

When we look at the benchmark results, it’s clear that First() is generally faster than Single().

First() method returns as soon as it finds the first matching element. If the match is near the beginning of the list (like in First_FirstElement), it’s extremely fast (7.408 ns). Even when the match is at the end (as in First_LastElement), it takes longer (7,959.958 ns) because it has to scan more elements.

On the other hand, Single() must check the entire list to ensure there’s exactly one matching element. This extra step makes it slower overall. Looking at the results, you can see that Single_FirstElement and Single_LastElement take longer regardless of the position of the matching element.

| Method              | Mean         | Error      | StdDev     |
|-------------------- |-------------:|-----------:|-----------:|
| First_FirstElement | 7.408 ns | 0.1154 ns | 0.1023 ns |
| First_LastElement | 7,959.958 ns | 20.2906 ns | 17.9871 ns |
| Single_FirstElement | 6,845.419 ns | 25.2963 ns | 22.4245 ns |
| Single_LastElement | 7,973.369 ns | 19.9738 ns | 17.7062 ns |

Validation Considerations

Now, what if you need to ensure that only a single piece of data is retrieved? In such cases, should you choose First() or Single()? The answer is Single(). Single() will throw an error if there is more than one matching element, ensuring there are no duplicates and thus maintaining data integrity. On the other hand, First() will return the first matching element even if duplicates exist, which does not guarantee data integrity.

Conclusion

First() is superior in terms of performance and is suitable when you need to find only the first element that meets a condition. However, Single() is a better choice when you need to ensure uniqueness and data integrity. Especially when you require a strict condition where only one matching element should exist, using Single() makes your code's intent clear and helps ensure that the data is processed correctly.

Understanding the differences between these methods is crucial. By knowing when to use First() and when to use Single(), you can write more efficient and reliable code. Choose the right method depending on your purpose and use it effectively.

--

--