2013-08-20 273 views
0

我有一個列表與類作爲結構如下從列表中與列表刪除重複作爲對象

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類的列表,我需要從該列表中刪除重複 可以任何一個幫助我,我怎麼能做到這一點

+2

你沒有'C1'類,也不是'c1'類,它是一個'cl'類。 –

+1

你是什麼意思重複?你的標準是什麼?同名1? –

+1

爲什麼你不使用字典? – Naren

回答

0

我想這是你的以後。

 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()); 
1

當引用c2List,同樣調用它的下面:

var distinctList = c1.c2List.Distinct(); 

(假設C1被進一步實例化了的代碼)