开发者

I am not able to compare two object in my asp.net mvc application

开发者 https://www.devze.com 2023-04-07 12:10 出处:网络
I have this code to compare two objects, these two out put results are same. but my equal condition is allw开发者_C百科ays getting false. I am not understanding is that something I am doign wrong here

I have this code to compare two objects, these two out put results are same. but my equal condition is allw开发者_C百科ays getting false. I am not understanding is that something I am doign wrong here?

var t1 = repo.Model_Test_ViewAllBenefitCodes(2).OrderBy(p => p.ba_Object_id).ToArray();//.FirstOrDefault();
            var t2 = x.ViewAllBenefitCodes.OrderBy(p => p.ba_Object_id).ToArray();//.FirstOrDefault();


            for (int i = 0; i < t1.Count(); i++)
            {
                var res1 = t1[i]==(t2[i]);
                var res = t1[i].Equals(t2[i]);


                Assert.AreEqual(res, true);
            }


It really depends on the object you're trying to compare, but this will compare classes that only have children (no grandchildren?) It's using reflection to pull all of the properties in the class and compare them.

    Private Function Compare(ByVal Obj1 As Object, ByVal Obj2 As Object) As Boolean
    'We default the return value to false
    Dim ReturnValue As Boolean = False

    Try
        If Obj1.GetType() = Obj2.GetType() Then

            'Create a property info for each of our objects
            Dim PropertiesInfo1 As PropertyInfo() = Obj1.GetType().GetProperties()
            Dim PropertiesInfo2 As PropertyInfo() = Obj2.GetType().GetProperties()

            'loop through all of the properties in the first object and compare them to the second
            For Each pi As PropertyInfo In PropertiesInfo1
                Dim CheckPI As PropertyInfo
                Dim CheckPI2 As PropertyInfo
                Dim Value1 As New Object
                Dim Value2 As New Object

                'We have to do this because there are errors when iterating through lists
                CheckPI = pi
                'Here we pull out the property info matching the name of the 1st object
                CheckPI2 = (From i As PropertyInfo In PropertiesInfo2 Where i.Name = CheckPI.Name).FirstOrDefault

                'Here we get the values of the property
                Value1 = CType(CheckPI.GetValue(Obj1, Nothing), Object)
                Value2 = CType(CheckPI2.GetValue(Obj2, Nothing), Object)

                'If the objects values don't match, it return false
                If Object.Equals(Value1, Value2) = False Then
                    ReturnValue = False
                    Exit Try
                End If
            Next

            'We passed all of the checks!  Great Success!
            ReturnValue = True

        End If
    Catch ex As Exception
        HandleException(ex)
    End Try

    Return ReturnValue
End Function


If a custom entity at your disposal, what I've done is override the Equals and GetHashCode to return an identifier of the object:

public override void Equals(object obj)
{
   if (obj == null || !(obj is MyObject))
       return false;

   return this.Key == ((MyObject)obj).Key;
}

public override int GetHashCode()
{
   return this.Key; 
   //or some other unique hash code combination, which could include
   //the type or other parameters, depending on your needs
}

This worked for me especially in scenarios with LINQ where the entities generated by the designer would not compare properly. I also sometimes have better luck with Object.Equals(obj1, obj2).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号