Class PropertyComparer<T, U>
A object comparer that can accept a lambda expression to compare properties.
Implements
Namespace: CapyKit
Assembly: CapyKit.dll
Syntax
public class PropertyComparer<T, U> : IEqualityComparer<T>
Type Parameters
| Name | Description |
|---|---|
| T | Generic type parameter of the parent object. |
| U | Generic type parameter of the property value. |
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.
Declaration
public PropertyComparer(Func<T, U> expression)
Parameters
| Type | Name | Description |
|---|---|---|
| Func<T, U> | expression | The expression. |
Methods
Equals(T, T)
Determines whether the specified properties are equal.
Declaration
public bool Equals(T x, T y)
Parameters
| Type | Name | Description |
|---|---|---|
| T | x | The first object of type |
| T | y | The second object of type |
Returns
| Type | Description |
|---|---|
| bool |
GetHashCode(T)
Returns a hash code for the specified object.
Declaration
public int GetHashCode(T obj)
Parameters
| Type | Name | Description |
|---|---|---|
| T | obj | The object for which a hash code is to be returned. |
Returns
| Type | Description |
|---|---|
| int | A hash code for the specified object. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | The type of |