[英]Remove duplicates from the list with lists as objects
我有一个类的列表如下
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; }
}
现在我有C1类的列表,我需要从该列表中删除重复项,任何人都可以帮助我该怎么做
引用c2List时,类似于以下内容进行调用:
var distinctList = c1.c2List.Distinct();
(假设在代码的上方进一步实例化了c1)
我想这就是你的追求。
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.