Samuel Jackson Samuel Jackson - 4 months ago 8
C# Question

How can I use LINQ on a List<T> where T doesn't have any nested elements?

Given the following:

List<int> foo = new List<int>() { 1, 2, 3, 5, 3, 1 };


How can I use LINQ (inline), in the following manner:

foo.Any( bar => ... )


I am having trouble with the
...
portion as I am used to using LINQ where
bar
is an object with nested elements like the following:

class Roo {
public int id { get; set; }
public string name { get; set; }
public Roo() { }
}

.....

List<Roo> foo = new List<Roo>() { ... };
foo.Any( bar => bar.id == 3; )

Answer

bar is the primitive type directly:

foo.Any( bar => bar > 1);

Think of LINQ extension method as for loops:

foreach(int bar in foo)
{

}

For objects:

foreach(Roo bar in foo)
{

}

I think this makes it easier for you.