Class PropertyComparer<T, U>
- Namespace
- CapyKit
- Assembly
- CapyKit.dll
A object comparer that can accept a lambda expression to compare properties.
public class PropertyComparer<T, U> : IEqualityComparer<T>
Type Parameters
TGeneric type parameter of the parent object.
UGeneric type parameter of the property value.
- Inheritance
-
PropertyComparer<T, U>
- Implements
- Inherited Members
- Extension Methods
Examples
using System; using System.Collections.Generic; using System.Linq;
class Program { static void Main(string[] args) { var people = new List<Person> { new Person { Name = "Alice", Age = 30 }, new Person { Name = "Bob", Age = 30 }, new Person { Name = "Charlie", Age = 35 } };
var comparer = new PropertyComparer<Person, int>(p => p.Age);
var distinctPeople = people.Distinct(comparer).ToList();
foreach (var person in distinctPeople)
{
Console.WriteLine($"{person.Name} - {person.Age}");
}
}
}
class Person { public string Name { get; set; } public int Age { get; set; } }
Constructors
PropertyComparer(Func<T, U>)
Constructor.
public PropertyComparer(Func<T, U> expression)
Parameters
expressionFunc<T, U>The expression.
Methods
Equals(T, T)
Determines whether the specified properties are equal.
public bool Equals(T x, T y)
Parameters
xTThe first object of type
Tto compare.yTThe second object of type
Tto compare.
Returns
GetHashCode(T)
Returns a hash code for the specified object.
public int GetHashCode(T obj)
Parameters
objTThe object for which a hash code is to be returned.
Returns
- int
A hash code for the specified object.
Exceptions
- ArgumentNullException
The type of
objis a reference type andobjis null.