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
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

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

bool

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 object for which a hash code is to be returned.

Returns

int

A hash code for the specified object.

Exceptions

ArgumentNullException

The type of obj is a reference type and obj is null.