EN
C# / .NET - filter List
0 points
In this article, we would like to show you how to filter Lists in C#.
Quick solution:
xxxxxxxxxx
1
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
2
3
List<int> filtered = numbers.Where(item => item > 2).ToList();
4
5
// Result:
6
// [3, 4, 5]
In this example, we use Where()
method from System.Linq
to get items greater than 2
from numbers
List.
xxxxxxxxxx
1
using System;
2
using System.Linq;
3
using System.Collections.Generic;
4
5
public class Program
6
{
7
public static void Main()
8
{
9
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
10
11
List<int> filtered = numbers.Where(item => item > 2).ToList();
12
13
foreach (int item in filtered)
14
Console.WriteLine(item);
15
}
16
}
Output:
xxxxxxxxxx
1
3
2
4
3
5