简体   繁体   English

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

[英]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.bazs
            .Select (x => x.ints)
            .Distinct ()
            .AsEnumerable ();

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

只需将您的.Select更改为.SelectMany以展平子列表:

.SelectMany (x => x.ints)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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