C# filter list
last modified November 15, 2026
This article shows how to filter a list in C#: with a loop, with the
FindAll and RemoveAll methods of
List<T>, and with LINQ.
Filtering selects the elements that satisfy a condition, a predicate. The approaches differ in what they return:
| Approach | Result |
|---|---|
foreach loop |
Whatever the loop builds, usually a new list |
List.FindAll |
A new List<T>, computed immediately |
LINQ Where, query expression |
A lazy IEnumerable<T>, computed when enumerated |
List.RemoveAll |
Modifies the list in place, returns the number of removed elements |
C# filter list with iteration
In the first example, we use a foreach loop to filter a list.
List<string> words = ["sky", "rock", "forest", "new", "falcon", "jewelry"];
List<string> filtered = [];
foreach (string word in words)
{
if (word.Length == 3)
{
filtered.Add(word);
}
}
Console.WriteLine(string.Join(", ", filtered));
The example selects the words that have three characters.
List<string> filtered = [];
The empty collection expression creates a new list for the results.
foreach (string word in words)
{
if (word.Length == 3)
{
filtered.Add(word);
}
}
We go over the words and add those that match the condition to the
filtered list. The loop is the most verbose approach, but it
is easy to debug and can do other work in the same pass.
$ dotnet run sky, new
C# filter list with FindAll
The FindAll method of List<T> returns a new
list with all elements that match a Predicate<T>. The
related Find, FindLast, and Exists
methods look for a single element.
List<int> vals = [-1, -3, 0, 1, 3, 2, 9, -4];
List<int> positive = vals.FindAll(e => e > 0);
Console.WriteLine(string.Join(", ", positive));
int first = vals.Find(e => e > 2);
int last = vals.FindLast(e => e < 0);
bool any = vals.Exists(e => e > 100);
Console.WriteLine($"First > 2: {first}, last < 0: {last}, any > 100: {any}");
The example selects the positive values and searches for single elements.
List<int> positive = vals.FindAll(e => e > 0);
The lambda expression is the predicate; it returns true for the
elements to keep.
int first = vals.Find(e => e > 2); int last = vals.FindLast(e => e < 0); bool any = vals.Exists(e => e > 100);
Find and FindLast return the first and the last
matching element. If there is none, they return the default value of the
type, which is 0 for int; this is indistinguishable from a
real 0 in the list. Exists tells whether any element
matches.
$ dotnet run 1, 3, 2, 9 First > 2: 3, last < 0: -4, any > 100: False
C# filter list with LINQ Where
The LINQ Where method works with any
IEnumerable<T>, not just lists. It does not create a new
list: it returns a query that filters the source each time it is
enumerated. This is called deferred execution. To get a list, call
ToList.
List<int> vals = [-1, -3, 0, 1, 3, 2, 9, -4];
IEnumerable<int> query = vals.Where(x => x > 0);
List<int> snapshot = vals.Where(x => x > 0).ToList();
vals.Add(42);
vals.Remove(9);
Console.WriteLine($"Query: {string.Join(", ", query)}");
Console.WriteLine($"Snapshot: {string.Join(", ", snapshot)}");
We create a query and a list from the same filter and then change the source.
IEnumerable<int> query = vals.Where(x => x > 0); List<int> snapshot = vals.Where(x => x > 0).ToList();
The query only stores the source and the predicate. ToList
runs the filter immediately and copies the results into a new list.
vals.Add(42); vals.Remove(9);
After the source changes, the query reflects the changes when we enumerate
it, while the snapshot keeps the original result. A query enumerated
several times also runs the filter several times; materialize it with
ToList if you need the results more than once. See the
LINQ deferred execution
article for more.
$ dotnet run Query: 1, 3, 2, 42 Snapshot: 1, 3, 2, 9
C# filter list with LINQ query expression
The query expression syntax expresses filtering with the
where clause. The compiler translates it to calls of the
Where method.
List<string> words = ["sky", "rock", "forest", "new", "falcon", "jewelry"];
var query = from word in words
where word.Length > 3
where word.Contains('o')
orderby word
select word.ToUpper();
foreach (string word in query)
{
Console.WriteLine(word);
}
We select words longer than three characters that contain the letter o.
var query = from word in words
where word.Length > 3
where word.Contains('o')
orderby word
select word.ToUpper();
A query can have several where clauses; an element must pass
all of them. The query syntax is convenient when filtering is combined with
sorting, projection, or joins.
$ dotnet run FALCON FOREST ROCK
C# filter a list of objects
Predicates usually test properties of objects. Pattern matching makes range conditions concise.
List<Car> cars =
[
new("Audi", 52642),
new("Mercedes", 57127),
new("Skoda", 9000),
new("Volvo", 29000),
new("Bentley", 350000),
new("Citroen", 21000),
new("Hummer", 41400),
new("Volkswagen", 21601)
];
var midRange = cars.Where(car => car.Price is > 9000 and < 50000);
foreach (Car car in midRange)
{
Console.WriteLine($"{car.Name} {car.Price}");
}
record Car(string Name, int Price);
We select the cars whose price is between 9000 and 50000, both exclusive.
var midRange = cars.Where(car => car.Price is > 9000 and < 50000);
The relational patterns combined with and mean the same as
car.Price > 9000 && car.Price < 50000. The Skoda
costs exactly 9000 and is not included.
$ dotnet run Volvo 29000 Citroen 21000 Hummer 41400 Volkswagen 21601
C# filter list with Func
A predicate is a value of type Func<T, bool>, so it can be
stored in a variable, returned from a method, or combined with other
predicates. This is useful when the filter is built at run time, for
example from the options of a search form.
List<User> users =
[
new(1, "John", "London", new DateOnly(2001, 4, 1)),
new(2, "Lenny", "New York", new DateOnly(1997, 12, 11)),
new(3, "Andrew", "Boston", new DateOnly(1987, 2, 22)),
new(4, "Peter", "Prague", new DateOnly(1936, 3, 24)),
new(5, "Anna", "Bratislava", new DateOnly(1973, 11, 18)),
new(6, "Albert", "Bratislava", new DateOnly(1940, 12, 11)),
new(7, "Adam", "Trnava", new DateOnly(1983, 12, 1)),
new(8, "Robert", "Bratislava", new DateOnly(1935, 5, 15)),
new(9, "Robert", "Prague", new DateOnly(1998, 3, 14))
];
Func<User, bool> LivesIn(string city) => u => u.City == city;
Func<User, bool> BornBefore(int year) => u => u.DateOfBirth.Year < year;
Func<User, bool> And(Func<User, bool> p1, Func<User, bool> p2) => u => p1(u) && p2(u);
foreach (User u in users.Where(LivesIn("Bratislava")))
{
Console.WriteLine(u);
}
Console.WriteLine("---");
var filter = And(LivesIn("Bratislava"), BornBefore(1950));
foreach (User u in users.Where(filter))
{
Console.WriteLine($"{u.Name}, born {u.DateOfBirth}");
}
record User(int Id, string Name, string City, DateOnly DateOfBirth);
We create predicates with factory functions and combine two of them.
Func<User, bool> LivesIn(string city) => u => u.City == city; Func<User, bool> BornBefore(int year) => u => u.DateOfBirth.Year < year;
The local functions return lambdas that capture their parameters. Each call creates a new predicate.
Func<User, bool> And(Func<User, bool> p1, Func<User, bool> p2) => u => p1(u) && p2(u);
The And function combines two predicates into one that
requires both.
var filter = And(LivesIn("Bratislava"), BornBefore(1950));
We select the users from Bratislava born before 1950. Calling
Where twice in a chain would have the same effect.
$ dotnet run
User { Id = 5, Name = Anna, City = Bratislava, DateOfBirth = 11/18/1973 }
User { Id = 6, Name = Albert, City = Bratislava, DateOfBirth = 12/11/1940 }
User { Id = 8, Name = Robert, City = Bratislava, DateOfBirth = 5/15/1935 }
---
Albert, born 12/11/1940
Robert, born 5/15/1935
C# filter list by index
An overload of Where passes the index of each element to the
predicate as a second argument.
List<string> words = ["sky", "rock", "forest", "new", "falcon", "jewelry", "owl"];
var evenPositions = words.Where((word, index) => index % 2 == 0);
Console.WriteLine(string.Join(", ", evenPositions));
List<int> temps = [12, 14, 13, 17, 19, 18, 21];
var rising = temps.Where((t, i) => i > 0 && t > temps[i - 1]);
Console.WriteLine(string.Join(", ", rising));
We select elements by their position and compare elements with their predecessors.
var evenPositions = words.Where((word, index) => index % 2 == 0);
We keep the words at indexes 0, 2, 4, and 6.
var rising = temps.Where((t, i) => i > 0 && t > temps[i - 1]);
We keep the temperatures that are higher than the previous one. The index lets the predicate look at neighbouring elements of the list.
$ dotnet run sky, forest, falcon, owl 14, 17, 19, 21
C# filter list by type
The OfType<T> method keeps the elements that are of the
given type and casts them to it. Since null has no type, it
also removes null values.
List<object> items = [1, "falcon", 2.5, "owl", 3, null!, 'c', "sky"];
var strings = items.OfType<string>();
Console.WriteLine(string.Join(", ", strings));
var ints = items.OfType<int>();
Console.WriteLine(string.Join(", ", ints));
List<string?> names = ["Jane", null, "Peter", null, "Lucy"];
List<string> present = names.OfType<string>().ToList();
Console.WriteLine($"{present.Count} names: {string.Join(", ", present)}");
var notNull = names.Where(n => n is not null);
Console.WriteLine(string.Join(", ", notNull));
We filter a list of mixed objects and a list of nullable strings.
var strings = items.OfType<string>();
The result is an IEnumerable<string>, so no further casts
are needed.
List<string> present = names.OfType<string>().ToList();
Applied to a list of string?, OfType removes the
nulls and gives a sequence of non-nullable strings. The equivalent
Where(n => n is not null) filters the same way, but its
element type remains string? for the compiler's null-state
analysis.
$ dotnet run falcon, owl, sky 1, 3 3 names: Jane, Peter, Lucy Jane, Peter, Lucy
C# filter list with RemoveAll
The RemoveAll method removes all elements that match a
predicate from the list itself. It returns the number of removed elements.
It runs in a single pass, which is faster than removing elements one by one
with Remove.
List<int> numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
int removed = numbers.RemoveAll(n => n % 2 == 0);
Console.WriteLine($"Removed {removed} elements");
Console.WriteLine(string.Join(", ", numbers));
List<string> words = ["sky", "", "rock", " ", "forest", ""];
for (int i = words.Count - 1; i >= 0; i--)
{
if (string.IsNullOrWhiteSpace(words[i]))
{
words.RemoveAt(i);
}
}
Console.WriteLine(string.Join(", ", words));
We remove the even numbers from a list and then the blank strings from another list with a loop.
int removed = numbers.RemoveAll(n => n % 2 == 0);
Unlike the other approaches, the predicate selects the elements to remove, not the ones to keep.
for (int i = words.Count - 1; i >= 0; i--)
{
if (string.IsNullOrWhiteSpace(words[i]))
{
words.RemoveAt(i);
}
}
If elements must be removed in a loop, the loop goes backwards, so that
removing an element does not shift the indexes of the elements still to be
checked. Removing elements inside a foreach loop throws an
InvalidOperationException.
$ dotnet run Removed 5 elements 1, 3, 5, 7, 9 sky, rock, forest
Source
In this article we have filtered lists in C# with a loop, the
FindAll and RemoveAll methods, LINQ
Where, query expressions, and OfType. See also the
List,
Predicate, and
LINQ tutorials.
Author
List all C# tutorials.