Collection Performance - How Do You LINQ?

Way, way back in 2007, the Language-Integrated Query (LINQ) was introduced as part of .NET 3.5. Since LINQ was new, I spent some time learning how to create the most performant queries, especially for Entity Framework. I learned back then how the query is written can affect performance. In this article, I will show the most performant way to write queries for a collection of objects.

The LINQ API & LAMBDA’S

In the beginning of LINQ API, this is how we wrote a simple where query:

var query = from person in PersonCollection
	       where person.Age.TotalDays > 1000
	       select person;
var result = query.ToList() // This runs the query

This same query can be written this way using a LAMBDA:

var result = PersonCollection.Where(p => p.Age.TotalDays > 1000).ToList();

Which one do you think is faster?

Collection Performance: How Do You LINQ?

The speed of these two ways to write a simple where query is too close to call. Let’s see how the performance is for other common LINQ methods.

Any()

This LINQ Any() method returns a Boolean if there is even one item in the collection that meets the criteria. There are three ways to use Any().

// API
var query = from person in PersonCollection
	       where person.Age.TotalDays > 1000
	       select person;	     
var result = query.Any();	     
// LAMBDA'S
var result = PersonCollection.Where(p => p.Age.TotalDays > 1000).Any();
OR
var result = PersonCollection.Any(p => p.Age.TotalDays > 1000);

Performance

Collection Performance: How Do You LINQ?

As you can see, using Any() as the first method in a LAMBDA call is faster!

First() & FirstOrDefault()

The method First() returns the first item in the collection that meets the criteria while FirstOrDefault() will also return a default value. Below are the different ways to use these methods.

// API
var query = from person in PersonCollection
	       where person.Age.TotalDays > 1000
	       select person;
var result = query.First();
var result = query.FirstOrDefault();
// LAMBDA'S
var result = PersonCollection.Where(p => p.Age.TotalDays > 1000).First();
var result = PersonCollection.Where(p => p.Age.TotalDays > 1000).FirstOrDefault();
OR
var result = PersonCollection.First(p => p.Age.TotalDays > 1000);
var result = PersonCollection.FirstOrDefault(p => p.Age.TotalDays > 1000);

Performance

Collection Performance: How Do You LINQ?

Collection Performance: How Do You LINQ?

As you can see, using First() or FirstOrDefault() as the first method a LAMBDA call is faster!

Last() & LastOrDefault()

The method Last() returns the last item in the collection that meets the criteria while LastOrDefualt() will also return a default value. Below are the different ways to use these methods.

// API
var query = from person in PersonCollection
	       where person.Age.TotalDays > 1000
	       select person;
var result = query.Last();
var result = query.LastOrDefault();
// LAMBDA'S
var result = PersonCollection.Where(p => p.Age.TotalDays > 1000).Last();
var result = PersonCollection.Where(p => p.Age.TotalDays > 1000).LastOrDefault();
OR
var result = PersonCollection.Last(p => p.Age.TotalDays > 1000);
var result = PersonCollection.LastOrDefault(p => p.Age.TotalDays > 1000);

Performance

Collection Performance: How Do You LINQ?

Collection Performance: How Do You LINQ?

As you can see, using Last() or LastOrDefault() as the first method in a LAMBDA call is much, much faster!

Summary

These benchmark results prove, without a doubt, that you should always use Any(), First(), FirstOrDefault, Last(), LastOrDefault or Where() at the beginning of a LAMBDA statement as shown below:

var result = PersonCollection.FirstOrDefault(p => p.Age.TotalDays > 1000);

I apologize if now you must go analyze your codebase for these methods and fix them if needed. It will increase the performance of your applications. If you have any comments, please make them below.


Similar Articles
McCarter Consulting
Software architecture, code & app performance, code quality, Microsoft .NET & mentoring. Available!