2014-02-24 58 views
-3

我有類似這樣從父列表忽略

Parent List {ChildList1, ChildList2, ChildList3, ChildList4} 
ChildList1: { "B1", "B2", "B3" } 
ChildList2: { "B1", "B3", "B2" } 
ChildList3: { "B1", "B2" } 
ChildList4: { "B5", "B3", "B2", "B4" } 

結構的順序刪除重複的孩子名單現在我想刪除具有類似(相同)值的孩子名單,並保持他們的只有一個副本在父列表中。

New Parent List {ChildList1, ChildList3, ChildList4} 

我更喜歡使用DISTINCT,我想知道如果我可以用它列出<>嗎?

List<List<string>> ParentList = new List<List<string>>(); 


List<string> ChildList1 = new List<string> { "B1", "B2", "B3" }; 
List<string> ChildList2 = new List<string> { "B1", "B3", "B2" }; 
List<string> ChildList3 = new List<string> { "B1", "B2" }; 
List<string> ChildList4 = new List<string> { "B5", "B3", "B2", "B4" }; 

ParentList.Add(ChildList1); 
ParentList.Add(ChildList2); 
ParentList.Add(ChildList3); 
ParentList.Add(ChildList4); 


var NewParentList = ParentList.Distinct(); 
// Display results. 

ChildList1ChildList2被認爲是平等的,只有一個需要保持。所以順序無關緊要。

+1

定義'類似的' – Bas

+0

@BasBrekelmans我在問題中包含了列表。這很明顯。 – Vahid

+0

@Vahid:什麼是顯而易見的?你已經顯示了一些列表,但你沒有提到你想保留的列表。什麼時候是一個重複的列表,如果一個項目匹配,兩個,三個,所有..?訂單問題等... –

回答

1

也許這可以幫助:

class Program 
{ 
    static void Main(string[] args) 
    { 
     List<List<string>> ParentList = new List<List<string>>(); 


     List<string> ChildList1 = new List<string> { "B1", "B2", "B3" }; 
     List<string> ChildList2 = new List<string> { "B1", "B3", "B2" }; 
     List<string> ChildList3 = new List<string> { "B1", "B2" }; 
     List<string> ChildList4 = new List<string> { "B5", "B3", "B2", "B4" }; 

     ParentList.Add(ChildList1); 
     ParentList.Add(ChildList2); 
     ParentList.Add(ChildList3); 
     ParentList.Add(ChildList4); 

     var result = ParentList.Distinct(new Comparer()).ToList(); 
    } 
} 

internal class Comparer : IEqualityComparer<List<string>> 
{ 
    public bool Equals(List<string> list1, List<string> list2) 
    { 
     return list1.All(x => list2.Contains(x)) && list2.All(x => list1.Contains(x)); 
    } 

    public int GetHashCode(List<string> obj) 
    { 
     return obj.Count; 
    } 
} 

當然,你必須使用更好的算法生成散列碼。它將刪除childList1或childList2,但不能保證哪一個被刪除。

+1

_「沒有保證哪一個被刪除」_對於LINQ-To-Objects,訂單是有保證的。 –

1

如果你想使用Distinct,你可以實現一個自定義IEqualityComparer<IEnumerable<T>>根據串忽略爲了找到重複,例如:

public class IgnoreSequenceOrderComparer<T> : IEqualityComparer<IEnumerable<T>> 
{ 
    public bool Equals(IEnumerable<T> x, IEnumerable<T> y) 
    { 
     if (x == null || y == null) 
      return false; 
     if (object.ReferenceEquals(x, y)) 
      return true; 
     return x.OrderBy(str => str).OrderBy(str => str) 
      .SequenceEqual(y.OrderBy(str => str).OrderBy(str => str)); 
    } 

    public int GetHashCode(IEnumerable<T> seq) 
    { 
     if (seq == null) return int.MinValue; 
     int hash = 0; 
     unchecked 
     { 
      var strings = seq.OrderBy(str => str); 
      foreach (T obj in strings) 
       hash = 17 * hash + obj.GetHashCode(); 
     } 
     return hash; 
    } 
} 

下面這段代碼刪除重複項:

var NewParentList = ParentList.Distinct(new IgnoreSequenceOrderComparer<string>());