有没有办法在 List中查找对象的属性 使用包含?
我在想如何才能知道列表中是否已存在某个对象。 我在列表中添加“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;
}
上面的第二个代码不起作用,我认为它是比较对象引用而不是对象内容/属性。
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;
}
是否有更小的方法可以在列表中查找对象的属性?
I was wandering how can I find out if an object already exists in my List.
I'm adding "newPerson" (instance of Person class) in a List, but checking if newPerson contents/properties exists or not in the List.
This piece works fine:
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;
}
First of all, I wanted to simplify/optimize this ugly code above. So I thought about using Contains method.
List<Person> people = this.GetPeople();
if (people.Contains<Person>(newPerson)) //it doesn't work!
{
MessageBox.Show("This person is already in the party!");
return;
}
The second code above doesn't work, I think it's comparing objects references and not object contents / properties.
Someone here on Stackoverflow and in link text was talking about using an class that implements IEqualityComparer. I gave it a try, but the code is much bigger now!
Something like:
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;
}
}
and to use this comparer:
PersonComparer comparer = new PersonComparer();
if (people.Contains<Person>(newPerson, comparer))
{
MessageBox.Show("This person is already in the party.");
return;
}
Is there a smaller way to find my object's properties in a List?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来您的 Person 类应该实现 IEquatable 。 是的,这是(有点)更多的代码,但是您不必每次想要比较 2 个人对象时都重复它。
列表的Contains方法默认使用对象的Equals方法。 因此,如果正确实现 IEquatable,则不必传递自定义 IEqualityComparer。
It sounds like your Person class should implement IEquatable<Person>. Yes, it is (a bit) more code, but then you don't have to repeat it each time you want to compare 2 person objects.
The Contains method of the list uses the Equals method of the object by default. So if you implement IEquatable correctly, you do not have to pass a custom IEqualityComparer.
将
Exists
或Any
与谓词一起使用:这将适用于 .NET 2.0(并且可以使用匿名方法转换为 C# 2)。 更多 LINQy 解决方案是
Any
:Use
Exists
orAny
with a predicate:That will work with .NET 2.0 (and can be converted to C# 2 using an anonymous method). The more LINQy solution is
Any
: