Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ: Creating an IEnumerable<T> from another IEnumerable<T>?

I have the following:

public class Foo
{
    public int x { get; set; }
}

public class Bar
{
    public void DoWork(IEnumerable<Foo> foos)
    {
        var enumOfX = ?;

        //Other code that uses enumOfX
    }
}

How can I create an IEnumerable<int> of all the x's?

like image 555
michael Avatar asked Jan 19 '23 06:01

michael


1 Answers

You use Select:

var enumOfX = foos.Select(foo => foo.x);

A huge amount of LINQ to Objects is creating one IEnumerable<T> from another... the rest is just aggregation :)

like image 195
Jon Skeet Avatar answered Jan 28 '23 09:01

Jon Skeet