Home > database >  Check if two instances of same class have same data in C#
Check if two instances of same class have same data in C#

Time:02-21

I'm trying to validate if both my objects have the same values.

here's my class

public class myclass
{
    public List<c1> List1{ get; set; }
    public List<c2> List2{ get; set; }
    public List<c3> List3{ get; set; }
}

public class c1
{
    public int Number{ get; set; }
    public string Name{ get; set; }
    public bool IsAvailable{ get; set; }
}

public class c2
{
    public int Id{ get; set; }
    public string Text{ get; set; }
    public GUID Guid{ get; set; }
}

public class c3
{
    public int? Age{ get; set; }
    public string Role{ get; set; }
    public bool IsDeleted{ get; set; }
}

I have 2 instances of this class and I want to compare the data between both the instances and check if they both are the same or not.

I've tried Serializing both the objects and comparing but that's not working as the items in the list can be in a different order.

I've also tried getting HashSet of individual lists and checking if they are equal.

var s1 = new HashSet<c1>(list1);
var s2= new HashSet<c1>(list2);
return s1.SetEquals(s2);

CodePudding user response:

One solution would be to first order the lists using a standard order, like numeric or alphabetic depending on the types, and then trying something like this

if (myclassInstance1.list1.SequenceEqual(myclassInstance2.list1))
{
    //Repeat the cycle 2 more times and then //your code
}

Since this one works comparing the sequence you need to first order the lists and to do that you can maybe use this

EDIT: This is the documentation for the list.sequenceEquals() method

CodePudding user response:

The best way to do this is if you explicitly implement the models with IEqualityComparer in .NET.

using System;
using System.Collections.Generic;
                    
public class Program
{
    public class MyClass
    {
        public List<C1> C1List { get; set; }
        public List<C2> C2List { get; set; }
    }
    
    public class C1 : IEqualityComparer<C1>
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        
        public bool Equals(C1 x, C1 y)
        {
            if(string.Equals(x.FirstName, y.FirstName, StringComparison.OrdinalIgnoreCase)
               && string.Equals(x.LastName, y.LastName, StringComparison.OrdinalIgnoreCase)) 
                return true;
            return false;
        }
        public int GetHashCode(C1 c)
        {
            return c.FirstName.Length * c.LastName.Length;
        }
    }
    
    public class C2 : IEqualityComparer<C2>
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        
        public bool Equals(C2 x, C2 y)
        {
            if(string.Equals(x.FirstName, y.FirstName, StringComparison.OrdinalIgnoreCase)
               && string.Equals(x.LastName, y.LastName, StringComparison.OrdinalIgnoreCase)) 
                return true;
            return false;
        }
        public int GetHashCode(C2 c)
        {
            return c.FirstName.Length * c.LastName.Length;
        }
    }
}

The IEqualityComparer will give you full control on "the parameters based on which you call 2 objects equal".

As for the above implementation, I have considered the 2 objects are equal if the FirstName and the LastName are same.

  • Related