简体   繁体   English

从列表中删除重复项,并将列表作为对象

[英]Remove duplicates from the list with lists as objects

I have one list with the class as a structure as below 我有一个类的列表如下

   class cl
    {
        public string name1{ get; set; }
        public string name2{ get; set; }
        public string name3{ get; set; }
        public List<c2> c2List{ get; set; }
    }
   class c2
    {
        public string st1{ get; set; }
        public string str2{ get; set; }
    }

Now I have list of C1 class and i need to remove duplicates from that list can any one help me how can i do it 现在我有C1类的列表,我需要从该列表中删除重复项,任何人都可以帮助我该怎么做

When referencing c2List, call it similarly to the below: 引用c2List时,类似于以下内容进行调用:

var distinctList = c1.c2List.Distinct();

(Assuming that c1 is instantiated further up the code) (假设在代码的上方进一步实例化了c1)

I think this is what your after.. 我想这就是你的追求。

        var c2Items = new c2[] 
        {
            new c2 { st1 = "value 1", str2 = "value 2" },
            new c2 { st1 = "value 2", str2 = "value 1" },
            new c2 { st1 = "value 1", str2 = "value 2" }
        };

        var parent = new cl() { c2List = new List<c2>(c2Items) };

        IEnumerable<c2> distinctitems = 
            parent
                .c2List
                .GroupBy(o => new { o.st1, o.str2 })
                .Select(o => o.First());

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

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