LINQ Filtering

This script can be used to filter collections through the use of LINQ.

Create a list(List<int> in the example below) and populate it in order to use said list as an example. Then apply two queries, one to filter the numbers bigger than 5 and the second one to order the items in a descending order, converting the result to a list.

List<int> list = new List<int>();
list.Add(1);
list.Add(2);
list.Add(-15);
list.Add(21);
list.Add(5);
list.Add(9);
list.Add(123);

var integerList = list.Where(x => x > 5);
var integerList = list.OrderByDescending(x => x).ToList();

Last updated