This post will discuss how to filter a list in C#.

1. Using Enumerable.Where() Method

A simple and elegant solution to filter a list is using LINQ. It has a Where() method that filters a sequence of values based on the specified predicate. The following code example demonstrates how we can use Where() for filtering a list.

Download  Run Code

 
Note that the above solution allocates a new list. The following code example uses the Where() clause to filter a sequence of objects by their age.

Download  Run Code

2. Using List<T>.FindAll() Method

The List<T>.FindAll() method retrieves all elements of the list that matches with the specified predicate delegate. We can use it as follows to filter a list in C#.

Download  Run Code

That’s all about filtering a list in C#.