2009-08-05 9 views
0

私のリストにオブジェクトが既に存在するかどうか調べるにはどうしたらいいですか? Listに「newPerson」(Personクラスのインスタンス)を追加しますが、newPersonのコンテンツ/プロパティがリストに存在するかどうかを確認します。Containsを使用してリスト<T>にオブジェクトのプロパティを見つける方法はありますか?

この作品は正常に動作します:すべての

 List<Person> people = this.GetPeople(); 
     if (people.Find(p => p.PersonID == newPerson.PersonID 
        && p.PersonName == newPerson.PersonName) != null) 
     { 
      MessageBox.Show("This person is already in the party!"); 
      return; 
     } 

まず、私はこの醜いコードは、上記の最適化/簡素化したかったです。そこでContainsメソッドの使用について考えました。

 List<Person> people = this.GetPeople(); 
     if (people.Contains<Person>(newPerson)) //it doesn't work! 
     { 
      MessageBox.Show("This person is already in the party!"); 
      return; 
     } 

上記の2番目のコードは機能しません。オブジェクト参照とオブジェクトの内容/プロパティを比較していると思います。

誰かがここでStackoverflowと​​でIEqualityComparerを実装するクラスの使用について話していた。私はそれを試してみましたが、コードは今よりはるかに大きいです!この比較演算子を使用する

public class PersonComparer : IEqualityComparer<Person> 
    { 
    // Products are equal if their names and i numbers are equal. 
    public bool Equals(Person x, Person y) 
    { 

     // Check whether the compared objects reference the same data. 
     if (Object.ReferenceEquals(x, y)) return true; 

     // Check whether any of the compared objects is null. 
     if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null)) 
      return false; 

     // Check whether the products' properties are equal. 
     return x.PersonID == y.PersonID && x.PersonName == y. PersonName; 
    } 

    // If Equals() returns true for a pair of objects, 
    // GetHashCode must return the same value for these objects. 

    public int GetHashCode(Person p) 
    { 
     // Check whether the object is null. 
     if (Object.ReferenceEquals(p, null)) return 0; 

     // Get the hash code for the Name field if it is not null. 
     int hashPersonName = p.PersonName == null ? 0 : p.PersonName.GetHashCode(); 
     int hashPersonID = i.PersonID.GetHashCode(); 

     // Calculate the hash code for the i. 
     return hashPersonName^hashPersonID; 
    } 

} 

と:: ような何か

 PersonComparer comparer = new PersonComparer(); 
     if (people.Contains<Person>(newPerson, comparer)) 
     { 
      MessageBox.Show("This person is already in the party."); 
      return; 
     } 

は、リストの私のオブジェクトのプロパティを見つけるための小さな方法はありますか?

+0

".net"と "c#"でタグ付けする必要があります – jpbochi

答えて

1

述語で使用Exists又はAny

List<Person> people = this.GetPeople(); 
if (people.Exists(p => p.PersonID == newPerson.PersonID 
         && p.PersonName == newPerson.PersonName)) 
{ 
    MessageBox.Show("This person is already in the party!"); 
    return; 
} 

.NET 2.0(匿名メソッドを使用して、C#2に変換することができる)で動作します。よりLINQyソリューションはAnyです:

List<Person> people = this.GetPeople(); 
if (people.Any(p => p.PersonID == newPerson.PersonID 
        && p.PersonName == newPerson.PersonName)) 
{ 
    MessageBox.Show("This person is already in the party!"); 
    return; 
} 
+0

はい私はそれを試してみましょう! Jonありがとう! –

3

あなたのPersonクラスはIEquatable<Person>を実装する必要がありますようですね。はい、それはもっとコードですが、2人のオブジェクトを比較するたびにそれを繰り返す必要はありません。

リストのContainsメソッドは、デフォルトでオブジェクトのEqualsメソッドを使用します。したがって、IEquatableを正しく実装すると、カスタムIEqualityComparerを渡す必要はありません。

関連する問題