C# LINQ Introduction
breigo
36.1K views
Filter data with Where
We've built up our understanding of LINQ using Where(predicate)
in the last section.
You can apply Where
to restrict the result set to contain only elements that satisfy a specified condition.
Example
Return all people, who are at least 30 years old.
Where
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// {
using System;
using System.Collections.Generic;
using System.Linq;
namespace Answer
{
public class FilterPeopleWithLinq
{
// }
public IEnumerable<Person> KeepAdults(IEnumerable<Person> people)
{
return people.Where(p => p.Age >= 30);
}
// {
}
}
// }
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.