List< T>でオブジェクトのプロパティを見つける方法はありますか?を使用していますか?

StackOverflow https://stackoverflow.com/questions/1235370

質問

リストにオブジェクトがすでに存在するかどうかを調べるにはどうすればいいのか迷っていました。 「newPerson」を追加しています(Personクラスのインスタンス)はリストにありますが、newPersonのコンテンツ/プロパティがリストに存在するかどうかを確認します。

この作品は問題なく動作します:

        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;
        }

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

役に立ちましたか?

解決

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;
}
scroll top