هل هناك طريقة للعثور على كائن خصائص في القائمة<T> باستخدام يحتوي على?
-
22-07-2019 - |
سؤال
أنا كان يتجول كيف يمكنني معرفة ما إذا كان كائن موجود بالفعل في القائمة.انا مضيفا "newPerson" (مثيل من فئة الشخص) في قائمة ، ولكن التحقق إذا 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<Person> people = this.GetPeople();
if (people.Contains<Person>(newPerson)) //it doesn't work!
{
MessageBox.Show("This person is already in the party!");
return;
}
الثاني رمز أعلاه لا يعمل, أعتقد أنها مقارنة الكائنات المراجع و ليس كائن المحتويات / خصائص.
شخص ما هنا على ستاكوفيرفلوو في وصلة النص كان يتحدث عن استخدام الفئة التي تطبق 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;
}
}
واستخدام هذه comparer:
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;
}
نصائح أخرى
هذا يبدو وكأنه من فئة الشخص أن تنفذ IEquatable<Person>.نعم هو (قليلا) رمز أكثر ، ولكن إذا لم يكن لديك لتكرار ذلك في كل مرة كنت ترغب في مقارنة 2 شخص الكائنات.
يحتوي على طريقة قائمة يستخدم أسلوب يساوي الكائن بشكل افتراضي.حتى إذا كنت تنفيذ IEquatable بشكل صحيح, ليس لديك لتمرير مخصص IEqualityComparer.