LINQ within a foreach()

 

An example of how LINQ where clauses save steps.


The where can occur right within the declaration of the foreach

LAMBDA syntax:

foreach (var person in people.Where(n => n.sex == "male"))
{
...
}

Query Syntax:

foreach (var person in from person in people where person.sex == "male" select person)
{
...
}

Both syntaxes compile to the same code

(Credit to @YuvalItzchakov https://stackoverflow.com/a/25412168 )


Comments

Popular posts from this blog

Default Browser - set url association with PowerShell

Rules of Automation