2011-06-27 13 views

答えて

1

IEqualityComparerを書き、行を表すオブジェクトのコレクションにEnumerable.Distinctを使用してください。

ような何か(申し訳ありませんが、コンパイラでテストしていない):

class Foo { 
    public int ValueA { get; set; } 
    public int ValueB { get; set; } 
    public int ValueC { get; set; } 
} 

class FooEqualityComparer : IEqualityComparer<Foo> { 
    public bool Equals(Foo x, Foo y) { 
     if(Object.ReferenceeEquals(x, y)) { return true; } 
     if(x == null || y == null) { return false; } 
     return x.ValueA == y.ValueA && 
       x.ValueB == y.ValueB && 
       x.ValueC == y.ValueC; 
    } 

    public int GetHashCode(Foo obj) { 
     if(obj == null) { return 0; } 
     unchecked { 
      int hashCode = 17; 
      hashCode = hashCode * 23 + obj.ValueA.GetHashCode(); 
      hashCode = hashCode * 23 + obj.ValueB.GetHashCode(); 
      hashCode = hashCode * 23 + obj.ValueC.GetHashCode(); 
      return hashCode; 
     } 
    } 
} 

その後:

IEnumerable<Foo> foos = // some foos; 
var distinct = foos.Distinct(new FooEqualityComparer()); 
0

あなたがあなた自身の比較演算子クラスに

class MyComparer : IEqualityComparer<YourRowClass> 

.Distinct()を使用することができます好きなように使う

yourList.Distinct(MyComparer()) 
関連する問題