Pages

Search

Wednesday, March 7, 2012

Comparing Data Tables



Below source helps to identify the datarows in a source table whose values are modified or different with respect to the similar row in the target table.

Below code, is only to identify the changes rows but not to identify the new inserted rows or deleted rows.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace Datasets
{
    class Program
    {
        static void Main(string[] args)
        {
            //creating 2 datasets,
            DataTable dt1 = new DataTable();
            dt1.Columns.Add("col1", typeof(System.Int32));
            dt1.Columns.Add("col2");

            DataRow dr = dt1.NewRow();
            dr["col1"] = 1;
            dr["col2"] = "Row1";
            dt1.Rows.Add(dr);

            dr = dt1.NewRow();
            dr["col1"] = 2;
            dr["col2"] = "Row2";
            dt1.Rows.Add(dr);

            DataTable dt2 = dt1.Copy();

            //changing the value of 2nd record
            dt2.Rows[0]["col2"] = null;
            DataSet d1 = new DataSet();
            DataSet d2 = new DataSet();
            d1.Tables.Add(dt1);
            d2.Tables.Add(dt2);
            CompareDataSets<int>(d1, d2, "col1");
        }

        //setting the primary column datatype as generic
        //sending the primary column name as argument
        static void CompareDataSets<T>(DataSet ds1, DataSet ds2, string strPrimaryColumn)
        {
            DataTable dtSrc = ds1.Tables[0];
            DataTable dtTar = ds2.Tables[0];
            var rws = (from r1 in dtSrc.AsEnumerable()
                       from r2 in dtTar.AsEnumerable()
                       where (r1.Field<T>(strPrimaryColumn).ToString().CompareTo(r2.Field<T>(strPrimaryColumn).ToString()) == 0)
                       && !r1.ItemArray.Compare(r2.ItemArray)
                       select r1
                  );
            //writing the id of the records which are differed
            foreach (var o in rws)
            {
                Console.WriteLine(o[strPrimaryColumn].ToString());
            }
            Console.Read();
        }
    }

    public static class ArrayExtension
    {
        //extension method to compare 2 arrays
        public static bool Compare(this Array Source, Array Target)
        {
            //checking if length are same
            if (Source.Length != Target.Length)
                return false;
            for (int i = 0; i < Source.Length; i++)
            {
                //checking if values are differed
                if (Source.GetValue(i) != null && Target.GetValue(i) != null)
                {
                    if (Source.GetValue(i).ToString().CompareTo(Target.GetValue(i).ToString()) != 0)
                        return false;
                }
                else
                {
                    if (Source.GetValue(i) == null && Target.GetValue(i) == null)
                        continue;
                    else
                        return false;
                }
            }
            return true;
        }
    }
}


No comments:

Post a Comment