Five LINQ Methods in Pictures
16 May 2022 #tutorial #csharpOne of the best C# features is LINQ. I would say it’s the most distinctive of all C# features. These are five of the most common LINQ method in pictures.
LINQ is the declarative, immutable, and lazy-evaluated way of working with collections in C#. Some frequently used LINQ methods are Where, Select, Any, GroupBy, and FirstOrDefault.
Let’s work with a list of our favorite movies. Let’s write a Movie
class with a name, release year, and a rating.
var movies = new List<Movie>
{
new Movie("Titanic", 1998, 4.5f),
new Movie("The Fifth Element", 1997, 4.6f),
new Movie("Terminator 2", 1991, 4.7f),
new Movie("Avatar", 2009, 5),
new Movie("Platoon", 1986, 4),
new Movie("My Neighbor Totoro", 1988, 5)
};
1. Where
The Where method returns a new collection with only the elements that meet a given condition.
The Where
method works like a filter on collections. Think of Where
as a replacement for a foreach
with an if
in it.
Let’s filter our list of movies to keep only those with a rating greater than or equal to 4.5.
var favorites = movies.Where(movie => movie.Rating >= 4.5);
This query would be something like this,
We’re using arrows to display our LINQ queries. But, the output of a LINQ query is lazy-evaluated. It means the actual result of a LINQ query is evaluated until we loop through its result.
2. Select
The Select method applies a function to transform every element of a collection.
Let’s find only the names of our favorite movies.
var namesOfFavorites = movies.Where(movie => movie.Rating >= 4.5)
.Select(movie => movie.Name);
This query would be,
3. Any
The Any method checks if a collection has at least one element matching a condition. Unlike Where and Select, Any doesn’t return a new collection, but either true or false.
Let’s see if we have watched movies with a low rating.
var hasBadMovies = movies.Any(movie => movie.Rating < 2);
This query would be,
4. GroupBy
The GroupBy method returns a collection of “buckets” organized by a key. Also, GroupBy transforms each bucket of elements.
Let’s count the films with the same rating.
var groupedByRating = movies.GroupBy(movie => movie.Rating,
(rating, moviesWithSameRating) => new
{
Rating = rating,
Count = moviesWithSameRating.Count()
});
The second parameter of the GroupBy
is a Func
with the grouping key and the elements of each group as parameters. This Func
works like a mapping function to transform each group or bucket found.
This query would be,
GroupBy has other use-cases, like grouping by more than one property.
5. First & FirstOrDefault
The First and FirstOrDefault methods return the first element in a collection or the first one matching a condition. Otherwise, First throws an exception, and FirstOrDefault returns the default value of the collection type.
Let’s find the oldest film we have watched.
var oldest = movies.OrderBy(movie => movie.ReleaseYear)
.First();
This query would be,
Voilà! These are five LINQ methods I use often: Where, Select, Any, Group, and FirstOrDefault. Of course, LINQ has more methods like Aggreate, Intersect, Union, and Except, and new overloads in .NET6. But, you will get your back covered with those five methods.
To learn about LINQ and other methods, check my quick guide to LINQ. All you need to know to start working with LINQ, in 15 minutes or less. For more C# content, check C# Definitive Guide for a list of subjects every intermediate C# developer should know. And, my top 10 best C# features for other cool C# features.
Happy LINQ time!