[英]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.