0

I have two class :

public class Customer
{
    public string FirstName { get; set; }

    public string LastName { get; set; }

    public bool isActif { get; set; }

    public Product[] Product { get; set; }
}

public class Product
{
    public string Id { get; set; }
}

And two instances :

var Customer1 = new Customer[]
{
    new Customer()
    {
        FirstName = "FirstName1",
        LastName = "LastName1",
        isActif = true,
        Product = new Product[]
        {
            new Product()
            {
                Id = "2",
                Date = DateTime.Now.Date
            }
        }
    },
    new Customer()
    {
        FirstName = "FirstName2",
        LastName = "LastName2",
        isActif = false,
        Product = new Product[]
        {
            new Product()
            {
                Id = "3",
                Date = DateTime.Now.Date
            }
        }
    }
};

var Customer2 = new Customer[]
{
    new Customer()
    {
        FirstName = "FirstName1",
        LastName = "LastName1",
        isActif = true,
        Product = new Product[]
        {
            new Product()
            {
                Id = "2",
                Date = DateTime.Now.Date
            }
        }
    }
};

I want to get the intersect of the two instances by contribution in the LastName. Is it possible to do this without implementing the method Equals() and GetHashCode() in my class ? I have a lot of properties.

Also, I would like to know the customers present in the first authority and not in the second. And conversely

  • Marked as duplicate because I think the highest upvoted answer there is exactly what you want. And otherwise it shows you that `IEqualityComparer` can be used for this kind of problems. – Gert Arnold May 22 '16 at 20:51
  • 1
    `Customer1.Except(Customer2, new LambdaComparer((c1,c2) => c1.LastName == c2.LastName))`. – Gert Arnold May 22 '16 at 21:39
  • Just look at the [documentation in MSDN](https://msdn.microsoft.com/en-us/library/bb338049%28v=vs.100%29.aspx). Many LINQ methods have overloads that accept an `IEqualityComparer `. Try to work through some examples to get the gist of it. – Gert Arnold May 22 '16 at 21:48

0 Answers0