简体   繁体   中英

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?

You useSelect :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM