Monday, October 10, 2011

How to Compare two datatable objects

An easy solution to compare two datatable object with the support of business object classes.

public static DataTable CompareObject(object x, object y)
        {
            //return if input objects are of not same type
            DataTable dtComp = new DataTable();
            DataColumn DtCol1 = new DataColumn("Field_Name");
            DataColumn DtCol2 = new DataColumn("Old_Value");
            DataColumn DtCol3 = new DataColumn("New_Value");
            dtComp.Columns.Add(DtCol1);
            dtComp.Columns.Add(DtCol2);
            dtComp.Columns.Add(DtCol3);
            Type type = x.GetType();
            Type type1 = y.GetType();
            if (!type.Equals(type1))
                return dtComp;
          
            PropertyInfo[] properties = type.GetProperties();
            FieldInfo[] fields = type.GetFields();
            int compareValue = 0;
            try
            {
                foreach (PropertyInfo property in properties)
                {
                    DataRow Drow = dtComp.NewRow();
                    //Logic to Compare a ArrayList Collection
                    if (property.PropertyType == typeof(System.Collections.ArrayList))
                    {
                    }
                    //Logic to Compare a ArrayList Collection
                    if (property.PropertyType.IsArray == true)
                    {                     

                    }
                    else if (property.PropertyType.IsPrimitive == false && property.PropertyType.IsClass == true)
                    {
                        try
                        {
                            int compare = Compare((object)property.GetValue(x, null), (object)property.GetValue(y, null));
                            if (compare != 0)
                            {                               
                                Drow[0] = property.Name;
                                Drow[1] = (object)property.GetValue(x, null).ToString();
                                Drow[2] = (object)property.GetValue(y, null);
                                dtComp.Rows.Add(Drow);
                                continue;
                            }
                        }
                        catch { }
                    }
                    IComparable valx = property.GetValue(x, null) as IComparable;
                    if (valx == null)
                        continue;
                    object valy = property.GetValue(y, null);
                    compareValue = valx.CompareTo(valy);
                    if (compareValue != 0)
                    {
                        Drow[0] = property.Name;
                        Drow[1] = (object)property.GetValue(x, null).ToString();
                        Drow[2] = (object)property.GetValue(y, null);
                        dtComp.Rows.Add(Drow);
                        continue;
                    }
                  
                }
                foreach (FieldInfo field in fields)
                {
                    DataRow Drow = dtComp.NewRow();
                    IComparable valx = field.GetValue(x) as IComparable;
                    if (valx == null)
                        continue;
                    object valy = field.GetValue(y);
                    compareValue = valx.CompareTo(valy);
                    if (compareValue != 0)
                    {
                        Drow[0] = field.Name;
                        Drow[1] = valx.ToString();
                        Drow[2] = valy.ToString();
                        dtComp.Rows.Add(Drow);
                        continue;
                    }
                  
                }
            }
            catch
            {
            }
            return dtComp;
        }



  public static int Compare(object x, object y)
        {
            //return if input objects are of not same type
            Type type = x.GetType();
            Type type1 = y.GetType();
            if (!type.Equals(type1))
                return 1;
          
            PropertyInfo[] properties = type.GetProperties();
            FieldInfo[] fields = type.GetFields();
            int compareValue = 0;
            try
            {
                foreach (PropertyInfo property in properties)
                {
                    //Logic to Compare a ArrayList Collection
                    if (property.PropertyType == typeof(System.Collections.ArrayList))
                    {
                        System.Collections.ArrayList List1 = new System.Collections.ArrayList();
                        System.Collections.ArrayList List2 = new System.Collections.ArrayList();
                        List1 = (System.Collections.ArrayList)property.GetValue(x, null);
                        List2 = (System.Collections.ArrayList)property.GetValue(y, null);
                        for (int count1 = 0; count1 < List1.Count && count1 < List2.Count; count1++)
                        {
                            if (!Object.Equals(List1[count1], List2[count1]))
                            {
                                compareValue = 1;
                                return compareValue;
                            }
                        }
                    }
                    //Logic to Compare a ArrayList Collection
                    if (property.PropertyType.IsArray == true)
                    {
                        object[] Obj1 = (object[])property.GetValue(x, null);
                        object[] Obj2 = (object[])property.GetValue(y, null);
                        for (int count1 = 0; count1 < Obj1.Length && count1 < Obj2.Length; count1++)
                        {
                            if (!Object.Equals(Obj1[count1], Obj2[count1]))
                            {
                                compareValue = 1;
                                return compareValue;
                            }
                        }

                    }
                    else if (property.PropertyType.IsPrimitive == false && property.PropertyType.IsClass == true)
                    {
                        try
                        {
                            int compare = Compare((object)property.GetValue(x, null), (object)property.GetValue(y, null));
                            if (compare != 0)
                            {
                                compareValue = compare;
                                return compare;
                            }
                        }
                        catch { }
                    }
                    IComparable valx = property.GetValue(x, null) as IComparable;
                    if (valx == null)
                        continue;
                    object valy = property.GetValue(y, null);
                    compareValue = valx.CompareTo(valy);
                    if (compareValue != 0)
                        return compareValue;
                }
                foreach (FieldInfo field in fields)
                {
                    IComparable valx = field.GetValue(x) as IComparable;
                    if (valx == null)
                        continue;
                    object valy = field.GetValue(y);
                    compareValue = valx.CompareTo(valy);
                    if (compareValue != 0)
                        return compareValue;
                }
            }
            catch
            {
            }
            return compareValue;
        }

No comments:

Post a Comment