Generics 有没有办法找到一个对象';列表中的属性<;T>;使用包含?

Generics 有没有办法找到一个对象';列表中的属性<;T>;使用包含?,generics,list,find,iequalitycomparer,Generics,List,Find,Iequalitycomparer,我想知道我的列表中是否已经存在一个对象。 我在列表中添加了“newPerson”(Person类的实例),但检查列表中是否存在newPerson内容/属性 这件作品很好: List<Person> people = this.GetPeople(); if (people.Find(p => p.PersonID == newPerson.PersonID && p.PersonName

我想知道我的列表中是否已经存在一个对象。 我在列表中添加了“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;
        }
List people=this.GetPeople();
if(people.Find)(p=>p.PersonID==newPerson.PersonID
&&p.PersonName==newPerson.PersonName)!=null)
{
Show(“此人已在聚会中!”);
返回;
}
首先,我想简化/优化上面这段难看的代码。所以我考虑使用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;
        }
List people=this.GetPeople();
if(people.Contains(newPerson))//它不工作!
{
Show(“此人已在聚会中!”);
返回;
}
上面的第二个代码不起作用,我认为它是在比较对象引用,而不是对象内容/属性

Stackoverflow和in上有人在谈论使用一个实现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;
    }

}
公共类人员比较者:IEqualityComparer
{
//如果产品的名称和编号相等,则产品相等。
公共布尔等于(人x,人y)
{
//检查比较对象是否引用相同的数据。
if(Object.ReferenceEquals(x,y))返回true;
//检查是否有任何比较对象为空。
if(Object.ReferenceEquals(x,null)| | Object.ReferenceEquals(y,null))
返回false;
//检查产品的属性是否相等。
返回x.PersonID==y.PersonID&&x.PersonName==y.PersonName;
}
//如果Equals()对一对对象返回true,
//GetHashCode必须为这些对象返回相同的值。
公共int GetHashCode(个人p)
{
//检查对象是否为空。
if(Object.ReferenceEquals(p,null))返回0;
//如果名称字段不为null,则获取其哈希代码。
int-hashPersonName=p.PersonName==null?0:p.PersonName.GetHashCode();
int hashPersonID=i.PersonID.GetHashCode();
//计算i的哈希代码。
返回hashPersonName^hashPersonID;
}
}
要使用此比较器:

        PersonComparer comparer = new PersonComparer();
        if (people.Contains<Person>(newPerson, comparer))
        {
            MessageBox.Show("This person is already in the party.");
            return;
        }
PersonComparer comparer=new PersonComparer();
if(people.Contains(newPerson,comparer))
{
MessageBox.Show(“此人已在聚会中。”);
返回;
}

有没有一种较小的方法可以在列表中查找对象的属性?

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

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

听起来您的Person类应该实现。是的,这是(一点)更多的代码,但是您不必每次都重复它来比较2个人对象


默认情况下,列表的Contains方法使用对象的Equals方法。因此,如果您正确地实现了IEquatable,就不必通过自定义IEqualityComparer。

听起来您的Person类应该实现。是的,这是(一点)更多的代码,但是您不必每次都重复它来比较2个人对象


默认情况下,列表的Contains方法使用对象的Equals方法。因此,如果您正确地实现了IEquatable,您不必通过自定义IEqualityComparer。

应该用“.net”和“c#”标记它,应该用“.net”和“c#”标记它。是的,我会试试!谢谢你,乔恩!是的,我要试一试!谢谢你,乔恩!