开发者

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

开发者 https://www.devze.com 2023-03-19 08:53 出处:网络
I have the following: public class Foo { public int x { get; set; } } publi开发者_StackOverflowc class Bar

I have the following:

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

publi开发者_StackOverflowc 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?


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 :)

0

精彩评论

暂无评论...
验证码 换一张
取 消