This page is a work in progress.You can help improve it. →

C# LINQ

We will be using this class as an example for exploring the various methods LINQ supplies.

public class Movie {
public int Id { get; set; }
public string Name { get; set; }
public string Tagline { get; set; }
public DateTime ReleasedDate { get; set; }
public int Screenings { get; set; }
public double PricePerTicket { get; set; }
public double TotalRevenue { get; set; }
public double Cost { get; set;}
public double Budget { get; set;}
}

The List we will be using is

var movies = new List<Movie>();

Things to pay attention to here.

Pay particular attention to both what the method returns (a new list of equal or smaller length, a single item, etc) and the logic the expression needs to implement. Understanding how the method works, the kind of data it returns, and what is expected of the expression is the key to being able to effectively use LINQ.

Select

We have seen an example of Select already. To define Select:

Makes a new list, of equal size, by running an expression on every item in the list and using that value when filling the new list.

Example:

// Make a list of strings with each string containing the year the corresponding Movie was released, a comma, and the title of the Movie.
var yearAndMovie = movies.Select(movie => $"{movie.ReleasedDate.Year}, {movie.Name} ");

Select, and many of the methods listed here, have a form where the expression can accept a second argument, the index of the element.

// Make a list of strings where each string contains the year the corresponding Movie was released, a comma, the title of the Movie, and the index in the original list.
var yearAndMovie = movies.Select((movie, index) => $"{movie.ReleasedDate.Year}, {movie.Name}, at index {index} ");

Where

The Where statement is like a filter. We use it when we want to make a new list, keeping some of the items from the original list.

Makes a new list, of equal or smaller size by running an expression against every item, keeping items when the expression returns true.

// Make a new list containing the movies that have over 100 Screenings
var popularMovies = movies.Where(movie => movie.Screenings >= 100);

Aggregate

The Aggregate method, often called reduce in other languages, takes the list and processes it down into a single value. Thus why it is often called reduce.

Returns a single value. It starts with a value we will call the current value. The given expression gets to use, one at a time, the current value and the item from the list, returning a new current value.

A good example is to be able to take a list and turn it into a total.

// Find the total revenue for all movies
var totalRevenue = movies.Aggregate(0.0, (currentTotal, movie) => currentTotal + movie.TotalRevenue);

NOTE: Aggregate is one of the most difficult of these methods to understand. Don't worry if you don't get how it works.

FindIndex

The FindIndex statement allows us to detect the first element of a collection and return the index of that element when found. If no match is found, FindIndex returns the value -1.

// Find the index of the first movie that has over 100 screenings. Will return -1 if there aren't any such movies.
var indexOfFirstMovieHavingOverOneHundredScreenings = movies.FindIndex(movie => movie.Screenings >= 100);

All

This returns a single bool which will be true if the expression is true for every element in the list.

Returns a boolean if the expression evaluates to true for every element in the list.

// Figure out if all the movies are old movies, before 1965
var areAllOldMovies = movies.All(movie => movie.ReleasedDate.Year < 1965);

Any

Returns a boolean if there is even a single element in the list that causes the expression to return true.

// Figure out if there is even a single old movie (before 1965) in our list
var areAnyOldMovies = movies.Any(movie => movie.ReleasedDate.Year < 1965);

Count

Returns an integer of items count of elements for which the expression returns true.

// Get count of movies that cost more than $10 to see.
var moviesThatCostMoreThanTenDollars = movies.Count(movie => movie.PricePerTicket > 10);

Sum

Returns an integer by adding up the value of the expression for each item.

// Get the total price of all movie's PricePerTicket. This goes through every movie, adding to a running total of PricePerTicket, placing the grand total in the variable totalPriceOfAllTickets
var totalPriceOfAllTickets = movies.Sum(movie => movie.PricePerTicket);

First

Returns a single element of the list which is the first item for which the expression returns true. If no item is found, an exception is thrown.

// Our favorite movie is Jaws, let's get it from the list if it is there. If it isn't we'll get an exception/error
var favoriteMovie = movies.First(movie => movie.Name == "Jaws");

FirstOrDefault

Returns a single element of the list which is the first item for which the expression returns true. If no item is found, the default value for that type is returned.

// Our favorite movie is Jaws, let's get it from the list if it is there. If it isn't we'll get a value of `null` for `favoriteMovie`
var favoriteMovie = movies.FirstOrDefault(movie => movie.Name == "Jaws");

Last

Returns the last item such that the expression returns true. If no item is found, an exception is thrown.

// Get the last item in the list that costs more than 10
var lastMovieCostingMoreThanTenDollars = movies.Last(movie => movie.PricePerTicket > 10);

LastOrDefault

Returns the last item such that the expression returns true. If no item is found, the default value for the type is returned.

// Get the last item in the list that costs more than 10. If no movie costs more than 10, then `lastMovieCostingMoreThanTenDollars` will be `null`
var lastMovieCostingMoreThanTenDollars = movies.LastOrDefault(movie => movie.PricePerTicket > 10);

Distinct

Returns all the distinct items in a list. This is commonly use in conjunction with a Select.

// Make a list of all the distinct movie titles. That is, if two movies have the same title, the title appears once.
var titles = movies.Select(movie => movie.Name).Distinct();

Max

Returns the highest value in the collection, but not the actual item. Useful for getting the largest value of a list of numbers, or the largest value of some property of a list of objects.

// Of all the values of `Budget` for all the movies, return the largest one.
var biggestBudget = movies.Max(movie => movie.Budget);

Min

Returns the smallest value in the collection, but not the actual item. Useful for getting numbers.

// Of all the values of `Budget` for all the movies, return the smallest one.
var smallestBudget = movies.Min(movie => movie.Budget);

Take

Returns a list of a specific number of items from the list.

// Make a new list of the first 8 movies from the list
var first8 = movies.Take(8);

Skip

Returns a part of the list that begins after skipping a number of items.

// Return a list of all the movies after skipping the first three
var afterTheFirst3 = movies.Skip(3);

OrderBy and OrderByDescending

Returns a new list of equal size in a sorted order based on the property returned by the expression.

// Makes a new list with all the movies ordered by their title
var alphabetically = movies.OrderBy(movie => movie.Name);

ThenBy and ThenByDescending

Used only after an OrderBy and resolves any OrderBy ties.

// Makes a new list sorted alphabetically by title, then by release year if they have the same Title
var sorted = movies.OrderBy(movie => movie.Name).ThenBy(movie => movie.ReleasedDate);

RemoveAll

Removes all items that the expression returns true, returning a new list of equal or smaller size.

// Return a list of all movies, except for, you know, that one, the one with the title we don't speak of.
var didNotHappen = movies.RemoveAll(movie => movie.Name == "Star Wars: Episode I – The Phantom Menace");

Here is a list of all the LINQ methods.

© 2017 - 2022; Built with ♥ in St. Petersburg, Florida.