Bienvenido Omosura
Bienvenido Omosura

Reputation: 127

Comparing two DataTables to determine if it is modified

I have two DataTables:

DataTable original; DataTable modified;

The two tables has the same number of rows and their columns and datas is the same.

I want my modified datable to determine each row to compare to the original datatable . If a row from the modified datatable is not equal to the row of original datatable then the row's .RowState will be set to .SetModified() from the modified datatable.

For Example:

...................................................

if row1 in orginal is not equal to row1 in modified then row1.SetModified()

if row2 in orginal is not equal to row2 in modified then row2.SetModified()....etc

............................................................

it should not be like this:

............................................................

if row1 in orginal is not equal to row2 in modified then row1.SetModified()

if row2 in orginal is not equal to row3 in modified then row1.SetModified()...etc

............................................................

get the idea? :)

Any code suggestion without using Primary Keys?

Upvotes: 2

Views: 4793

Answers (4)

HarryQuake
HarryQuake

Reputation: 163

I think following logic is better.

static void CompareRows(DataTable original, DataTable modified)
{
    foreach (DataRow row1 in modified.Rows)
    {
        bool isModified = true;
        var array1 = row1.ItemArray;
        foreach (DataRow row2 in original.Rows)
        {
            var array2 = row2.ItemArray;

            if (array1.SequenceEqual(array2))
            {
                isModified = false;
            }
        }
        if (isModified)
            row1.SetModified();
    }
}

Upvotes: 1

Sarvesh Mishra
Sarvesh Mishra

Reputation: 2072

Using foreach loop within another foreach is N X N comparison, that you don't need to do.

Comparing First row with First row of other table, second with second and so on using Zip extension method is very useful for this case.

DataTable original;
DataTable modified;

// your stuff

modified = modified.AsEnumerable().Zip<DataRow, DataRow, DataRow>(original.AsEnumerable(), (DataRow modif, DataRow orig) =>
{
    if (!orig.ItemArray.SequenceEqual<object>(modif.ItemArray))
    {
        modif.SetModified();
    }
    return modif;
}).CopyToDataTable<DataRow>();

Upvotes: 4

Gericke
Gericke

Reputation: 2241

Have a look at this code. You can play around with it to see if it will fit your needs:

class Program
{
    static void CompareRows(DataTable table1, DataTable table2)
    {
        foreach (DataRow row1 in table1.Rows)
        {
            foreach (DataRow row2 in table2.Rows)
            {
                var array1 = row1.ItemArray;
                var array2 = row2.ItemArray;

                if (array1.SequenceEqual(array2))
                {
                    Console.WriteLine("Equal: {0} {1}",
                    row1["Drug"], row2["Drug"]);
                }
                else
                {
                    Console.WriteLine("Not equal: {0} {1}",
                    row1["Drug"], row2["Drug"]);
                }
            }
        }
    }

    static DataTable GetTable1()
    {
        DataTable table = new DataTable();
        table.Columns.Add("Dosage", typeof(int));
        table.Columns.Add("Drug", typeof(string));
        table.Columns.Add("Patient", typeof(string));

        table.Rows.Add(25, "Indocin", "David");
        table.Rows.Add(50, "Enebrel", "Cecil");
        return table;
     }

     static DataTable GetTable2()
     {
         DataTable table = new DataTable();
         table.Columns.Add("Dosage", typeof(int));
         table.Columns.Add("Drug", typeof(string));
         table.Columns.Add("Patient", typeof(string));

         table.Rows.Add(21, "Combivent", "Janet");
         table.Rows.Add(50, "Enebrel", "Cecil");
         table.Rows.Add(10, "Hydralazine", "Christoff");
         return table;
     }

     static void Main()
     {
         CompareRows(GetTable1(), GetTable2());
     }
 }

Upvotes: 3

Manoj
Manoj

Reputation: 5071

Try this

foreach (DataRow row1 in original.Rows)
    {
        foreach (DataRow row2 in modified.Rows)
        {
            var array1 = row1.ItemArray;
            var array2 = row2.ItemArray;

            if (array1.SequenceEqual(array2))
            {
              // Your logic
            }
            else
            {
              // Your logic
            }
        }
    }

Reference

Upvotes: 3

Related Questions