Linq选择具有鲜明的内在价值

时间:2011-05-06 10:27:43

标签: c# linq

    class Foo 
    {
        public List<Baz> bazs = new List<Baz> ();
    }

    class Baz
    {
        public List<int> ints = new List<int> ();
    }

    [Test] public void play ()
    {
        var foo = new Foo ();

        foo.bazs = new List<Baz> ()
        {
            new Baz ()
            {
                ints = new List<int> () {1, 2, 3, 4, 5}
            },
            new Baz ()
            {
                ints = new List<int> () {4, 5, 6, 7, 8}
            }
        };

        IEnumerable<int> result = foo.bazs
            .Select (x => x.ints)
            .Distinct ()
            .AsEnumerable ();

        // I'm expecting an IEnumerable<int> 1,2,3,4,5,6,7,8
    }

1 个答案:

答案 0 :(得分:8)

只需将.Select更改为.SelectMany即可展平子列表:

.SelectMany (x => x.ints)