CapyKit/Docs/api/CapyKit.PropertyComparer-2.md

148 lines
4.3 KiB
Markdown

# <a id="CapyKit_PropertyComparer_2"></a> Class PropertyComparer<T, U\>
Namespace: [CapyKit](CapyKit.md)
Assembly: CapyKit.dll
A object comparer that can accept a lambda expression to compare properties.
```csharp
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](https://learn.microsoft.com/dotnet/api/system.object) ←
[PropertyComparer<T, U\>](CapyKit.PropertyComparer\-2.md)
#### Implements
[IEqualityComparer<T\>](https://learn.microsoft.com/dotnet/api/system.collections.generic.iequalitycomparer\-1)
#### Inherited Members
[object.Equals\(object?\)](https://learn.microsoft.com/dotnet/api/system.object.equals\#system\-object\-equals\(system\-object\)),
[object.Equals\(object?, object?\)](https://learn.microsoft.com/dotnet/api/system.object.equals\#system\-object\-equals\(system\-object\-system\-object\)),
[object.GetHashCode\(\)](https://learn.microsoft.com/dotnet/api/system.object.gethashcode),
[object.GetType\(\)](https://learn.microsoft.com/dotnet/api/system.object.gettype),
[object.MemberwiseClone\(\)](https://learn.microsoft.com/dotnet/api/system.object.memberwiseclone),
[object.ReferenceEquals\(object?, object?\)](https://learn.microsoft.com/dotnet/api/system.object.referenceequals),
[object.ToString\(\)](https://learn.microsoft.com/dotnet/api/system.object.tostring)
#### Extension Methods
[ObjectExtensions.UpdateProperties<PropertyComparer<T, U\>\>\(PropertyComparer<T, U\>, PropertyComparer<T, U\>\)](CapyKit.Extensions.ObjectExtensions.md\#CapyKit\_Extensions\_ObjectExtensions\_UpdateProperties\_\_1\_\_\_0\_\_\_0\_),
[ObjectExtensions.UpdateProperties\(object, object\)](CapyKit.Extensions.ObjectExtensions.md\#CapyKit\_Extensions\_ObjectExtensions\_UpdateProperties\_System\_Object\_System\_Object\_)
## Examples
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main(string[] args)
{
var people = new List&lt;Person&gt;
{
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
### <a id="CapyKit_PropertyComparer_2__ctor_System_Func__0__1__"></a> PropertyComparer\(Func<T, U\>\)
Constructor.
```csharp
public PropertyComparer(Func<T, U> expression)
```
#### Parameters
`expression` [Func](https://learn.microsoft.com/dotnet/api/system.func\-2)<T, U\>
The expression.
## Methods
### <a id="CapyKit_PropertyComparer_2_Equals__0__0_"></a> Equals\(T, T\)
Determines whether the specified properties are equal.
```csharp
public bool Equals(T x, T y)
```
#### Parameters
`x` T
The first object of type <code class="typeparamref">T</code> to compare.
`y` T
The second object of type <code class="typeparamref">T</code> to compare.
#### Returns
[bool](https://learn.microsoft.com/dotnet/api/system.boolean)
<a href="https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/bool">true</a> if the specified objects are equal; otherwise,
<a href="https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/bool">false</a>.
### <a id="CapyKit_PropertyComparer_2_GetHashCode__0_"></a> GetHashCode\(T\)
Returns a hash code for the specified object.
```csharp
public int GetHashCode(T obj)
```
#### Parameters
`obj` T
The <xref href="System.Object" data-throw-if-not-resolved="false"></xref> for which a hash code is to be returned.
#### Returns
[int](https://learn.microsoft.com/dotnet/api/system.int32)
A hash code for the specified object.
#### Exceptions
[ArgumentNullException](https://learn.microsoft.com/dotnet/api/system.argumentnullexception)
The type of <code class="paramref">obj</code> is a reference type and
<code class="paramref">obj</code> is
<a href="https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null">null</a>.