开发者

Linq selecting inner values with distinct

    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.b开发者_如何转开发azs
            .Select (x => x.ints)
            .Distinct ()
            .AsEnumerable ();

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


Simply change your .Select to .SelectMany to flatten the sublists:

.SelectMany (x => x.ints)
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜