Skip to main content

LINQ Tutorials

Tutorial Home
#2 Basic looking for something with Where(...) and Any(...)

Where(FirstName == "Terry")

This filters the authors to only find Terry.

  1. Terry Pratchett

1
2
3
4
5
6
<ol>
@foreach(var person in persons.Where(p => p.FirstName == "Terry")) {
<li>@person.FirstName @person.LastName</li>
}
</ol>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Where(FirstName.Length > 5)

This filters the authors with long first names.

  1. Douglas Adams
  2. George Akerlof
  3. Raphael Müller (not an author)

1
2
3
4
5
6
<ol>
@foreach(var person in persons.Where(p => p.FirstName.Length > 5)) {
<li>@person.FirstName @person.LastName</li>
}
</ol>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Any(FirstName.Length > 5 / 10)

This filters the authors with long first names.

  1. Persons with 5-char names or more: True
  2. Persons with 10-char names or more: False

1
2
3
4
5
6
7
8
9
<ol>
<li>
Persons with 5-char names or more: @persons.Any(p => p.FirstName.Length > 5)
</li>
<li>
Persons with 10-char names or more: @persons.Any(p => p.FirstName.Length > 10)
</li>
</ol>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#2 Basic looking for something with Where(...) and Any(...)

Source Code of this file

Below you'll see the source code of the file. Note that we're just showing the main part, and hiding some parts of the file which are not relevant for understanding the essentials. Click to expand the code

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX