cmrhema
cmrhema

Reputation: 981

How do I compare two datatables

I have a datatable that will consist of 72 columns.

I will download it in the excel sheet using VSTO, which works fine.

Now the user will change either one of these rows or all of these rows and will also insert a fresh row.

Considering the datatable downloaded first to be dtA, and the one that has been modified in the excel sheet to be dtB.

I want to compare dtA and dtB.

I need to find out all the rows in dtB that do not exist in dtA.

I cant put foreach loop for each and every single row and evaluate as its a very untidy way of coding.

What is a better way to do this?

I did this way,

    DataTable dtA = new DataTable();
    dtA.Columns.Add("ENo");
    dtA.Columns.Add("ENo1");
    dtA.Columns.Add("ENo2");
    dtA.Columns.Add("ENo3");
    dtA.Columns.Add("ENo4");

    for (int i = 0; i < 5; i++)
    {
        DataRow dr = dtA.NewRow();
        dr[0] = "Part 0 " + i.ToString();
        dr[1] = "Part 1 " + i.ToString();
        dr[2] = "Part 2 " + i.ToString();
        dr[3] = "Part 3 " + i.ToString();
        dr[4] = "Part 4 " + i.ToString();
        dtA.Rows.Add(dr);
    }

    DataTable dtB = new DataTable();
    dtB.Columns.Add("ENo");
    dtB.Columns.Add("ENo1");
    dtB.Columns.Add("ENo2");
    dtB.Columns.Add("ENo3");
    dtB.Columns.Add("ENo4");

    for (int i = 5; i < 10; i++)
    {
        DataRow dr = dtB.NewRow();
        dr[0] = "Part 0 " + i.ToString();
        dr[1] = "Part 1 " + i.ToString();
        dr[2] = "Part 2 " + i.ToString();
        dr[3] = "Part 3 " + i.ToString();
        dr[4] = "Part 4 " + i.ToString();
        dtB.Rows.Add(dr);
    }

    Response.Write("\n");
    Response.Write("dt A");
    Response.Write("\n");

    for (int i = 0; i < dtA.Rows.Count; i++)
    {
        Response.Write(dtA.Rows[i][i].ToString());
        Response.Write("\n");
    }

    Response.Write("\n");
    Response.Write("dt B");
    Response.Write("\n");
    for (int i = 0; i < dtB.Rows.Count; i++)
    {
        Response.Write(dtB.Rows[i][i].ToString());
        Response.Write("\n");
    }

    var VarA = dtA.AsEnumerable();
    var varB = dtA.AsEnumerable();

    var diff = VarA.Except(varB);
    Response.Write("except");
    foreach (var n in diff)
    {
        Response.Write(n.Table.Rows[0].ToString());

    }

But I do not know what to use in the foreach var, What should I use pls?

Upvotes: 2

Views: 1750

Answers (3)

bart
bart

Reputation: 7767

SELECT dtB.* FROM dtB LEFT JOIN dtA ON dtB.id=dtA.id /* AND ...  */
WHERE dtA.id IS NULL

You'll only see rows from dtB that don't have a match in dtA.

You can add whatever columns that need to be identical to count as the same row in the JOIN condition.

Depending on the database, there could be simpler ways. For example, Oracle has the MINUS keyword, to remove just entirely identical rows; rows that have been modified will still appear in the result.

Alternatively: you can export both Excel files as text (CSV, tab delimited, ...), provided they're in the same row order, and use the command line utility diff to see what's changed.

There could be a library available for your programming language that implements the diff algorithm, so you can do it all in memory.

Upvotes: 0

user166390
user166390

Reputation:

Perhaps LINQ for DataSets (this just represents the fact that DataSets support LINQ and is part of the .NET 3.5+ framework) may be an option. In particlar, check out the ExceptRows Set Pattern which just uses IEnumerable.Except. It is easiest if the row contains some "unique ID", but it should be sufficient to cover cases where the row itself is uniquely missing and/or detect if any row is modified.

There is also QueryADataSet -- the only product I know to "support SQL syntax on Data[Set|Table]" stuff (although it could be [weakly] argued that LINQ provides "SQL syntax"). Development licenses are $200 a pop, but it might be worth it, depending. (I have no affiliation with, nor have I tried the product; I stumbled upon it when trying to find a solution to a similar problem but ultimately just changed my approach.)

Happy coding.

Upvotes: 0

James
James

Reputation: 4152

SELECT id FROM dtB WHERE id NOT IN (SELECT id FROM dtA)

Upvotes: 1

Related Questions