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
T
Generic type parameter of the parent object.
U
Generic type parameter of the property value.
Inheritance
object ← PropertyComparer<T, U>
Implements
Inherited Members
object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.MemberwiseClone(), object.ReferenceEquals(object?, object?), object.ToString()
Extension Methods
ObjectExtensions.UpdateProperties<PropertyComparer<T, U>>(PropertyComparer<T, U>, PropertyComparer<T, U>), ObjectExtensions.UpdateProperties(object, object)
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
expression Func<T, U>
The expression.
Methods
Equals(T, T)
Determines whether the specified properties are equal.
public bool Equals(T x, T y)
Parameters
x T
The first object of type T to compare.
y T
The second object of type T to compare.
Returns
true if the specified objects are equal; otherwise, false.
GetHashCode(T)
Returns a hash code for the specified object.
public int GetHashCode(T obj)
Parameters
obj T
The System.Object for which a hash code is to be returned.
Returns
A hash code for the specified object.
Exceptions
The type of obj is a reference type and
obj is
null.