> For the complete documentation index, see [llms.txt](https://gabriel-schade-cardoso.gitbook.io/tango/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gabriel-schade-cardoso.gitbook.io/tango/extensions/equalitycomparerbuilder.md).

# EqualityComparer Builder

> [`Tango.Linq.EqualityComparerBuilder<T>`](https://github.com/gabrielschade/Tango/blob/master/Tango/Tango/Linq/EqualityComparerBuilder.cs)

This class implements [`IEqualityComparer<T>`](https://msdn.microsoft.com/pt-br/library/ms132151%28v=vs.110%29.aspx) interface and provides a method that can be used to create concrete objects that implements this comparison interface.

You can use this builder to define a dynamic comparison between two objects, you can inform this comparer as parameter for [`Distinct`](https://msdn.microsoft.com/pt-br/library/bb348436%28v=vs.110%29.aspx) method from [`System.Linq`](https://msdn.microsoft.com/pt-br/library/system.linq%28v=vs.110%29.aspx) namespace.

It's a better option than implements interface directly in class that you want to compare, because you can define multiple ways to compare same object types.

## Properties

| Name           | Type              | Description                                  |
| -------------- | ----------------- | -------------------------------------------- |
| Comparer       | Func\<T, T, bool> | Method used by Equals interface method.      |
| HashCodeGetter | Func\<T, int>     | Method used by GetHashCode interface method. |

## Methods

| Name   | Parameter                                                            | Returns                     | Description                                                 |             |       |     |                                         |
| ------ | -------------------------------------------------------------------- | --------------------------- | ----------------------------------------------------------- | ----------- | ----- | --- | --------------------------------------- |
| Create | <p>Func\<T, T, bool> comparer</p><p>Func\<T, int> hashCodeGetter</p> | EqualityComparerBuilder\<T> | Creates a new object that implements IEqualityComparer\<T>. |             |       |     |                                         |
| Equals | <p>T x</p><p>T y</p>                                                 | bool                        | IEqualityComparer\<T> interface method.                     | GetHashCode | T obj | int | IEqualityComparer\<T> interface method. |

## Usage

You can use the *static* method `Create` to creates a new instance of a comparer object.

See the code:

```csharp
class Product
{
   public int Id { get; set; }
   public string Description { get; set; }
}

Func<Product, Product, bool> compareProducts = 
      (product1, product2) => product1.Id == product2.Id;

Func<Product, int> getHashCodeProduct =
      product => product.Id.GetHashCode();

var comparer = 
    EqualityComparerBuilder<Product>.Create(
        compareProducts, 
        getHashCodeProduct);

IEnumerable<Product> products = GetProducts();
products.Distinct(comparer);
```

In the previous example, the `Distinct` method will no longer comparer the object referece. The comparison will be made by comparisons of `comparer` object.

You can use anonymous methods directly as `Create` method parameters:

```csharp
class Product
{
   public int Id { get; set; }
   public string Description { get; set; }
}
IEnumerable<Product> products = GetProducts();
products.Distinct(
    EqualityComparerBuilder<Product>.Create(
            (product1, product2) => product1.Id == product2.Id, 
            product => product.Id.GetHashCode());
);
```
