You must Sign In to post a response.
  • Category: ASP.NET

    Data row comparison with 2 datatables

    I have 2 datatable..

    From datatable1, with certain conditions I have retrieved data row array as follows:
    Datarow [] drow= datatable1.asenumerable ().where ().select ();

    Now assume these drow has 3 rows and im trying to search these rows in another datatable2
    Where I'm facing error.

    Int indecx= datatable2.rows.index (drow [0])// ERROR

    HOW TO SOLVE
  • #769285
    In order compare any two DataTable, you can use below method 'CompareDataTable'. SequenceEqual is used to check the value if the arrays are exactly equal in length and element composition.

    Protected void CompareDataTable(DataTable dtFIRST, DataTable dtSECOND)
    {
    foreach (DataRow row1 in dtFIRST.Rows)
    {
    foreach (DataRow row2 in dtSECOND.Rows)
    {
    var array1 = row1.ItemArray;
    var array2 = row2.ItemArray;

    if (array1.SequenceEqual(array2))
    {
    Console.WriteLine("Similar value found: {0} {1}",
    row1["Column1"], row2["Column2"]);
    }
    else
    {
    Console.WriteLine("Not Similar: {0} {1}",
    row1["Column1"], row2["Column1"]);
    }
    }
    }


Sign In to post your comments