开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜