[RESOLVED]Difference between two Data Tables

Table1                                       Table2

Person Name                           Person Name    Fees Paid

Chandru                                     Chandru                5000

Ramya                                        Ramya                  7000

Charu                                                             

Comparing two Data Tables, I want list the person name who has not paid ie Charu.

Pl suggest.

Why this thread is in html forum? From the description it looks like you have 2 datatables.

var x = Table2.AsEnumerable().Select(r => r.Field<string>("PersonName"))
        .Except(Table1.AsEnumerable().Select(r => r.Field<string>("PersonName")));
DataTable Table3 = (from row in Table2.AsEnumerable()
                   join PersonName in x
                   on row.Field<string>("PersonName") equals PersonName
                   select row).CopyToDataTable();

Source: http://stackoverflow.com/questions/15713243/compare-two-datatables-and-select-the-rows-that-are-not-present-in-second-table 

Hello,

For this, definitely you filled these two data table from db with two different tables. Why you are not filling single datatable with 

the below query? Correct if I am wrong.

    Select t1.PersonName from Table1 t1, Table2 t2 where t1.PersonName   Not Equal t2.PersonName

Thanks…

You can use the Except method to find the difference between two datatables.

Sample Code:

 IEnumerable<string> idsInA = table1.AsEnumerable().Select(row => (string)row["PersonName"]);
 IEnumerable<string> idsInB = table2.AsEnumerable().Select(row => (string)row["PersonName"]);
 //Get the user who dont paid fee 
 IEnumerable usernotpaidfeee = idsInA.Except(idsInB);

Complete Code:

                //Sample Data
                DataTable table1 = new DataTable();
                table1.Columns.Add("PersonName", typeof(string));
                table1.Rows.Add("Chandru");
                table1.Rows.Add("Ramya");
                table1.Rows.Add("Charu");

                DataTable table2 = new DataTable();
                table2.Columns.Add("PersonName", typeof(string));
                table2.Columns.Add("FeesPaid", typeof(int));
                table2.Rows.Add("Chandru",3000);
                table2.Rows.Add("Ramya",4000);

                DataTable table3 = new DataTable();
                IEnumerable<string> idsInA = table1.AsEnumerable().Select(row => (string)row["PersonName"]);
                IEnumerable<string> idsInB = table2.AsEnumerable().Select(row => (string)row["PersonName"]);
                //Get the user who dont paid fee 
                IEnumerable usernotpaidfeee = idsInA.Except(idsInB);

Hi Uma Sengoden,

Thanks for your post.

Uma Sengoden

Comparing two Data Tables, I want list the person name who has not paid ie Charu.

As for your problem, I agree with A2H. we could use Except(IEnumerable<T>) metod
to compare two sequences of integers and return elements that appear only in the first sequence.

For more information about Except(IEnumerable<T>) metod, you could refer to the following link.

if you have any question about this issue, you could post back freely.

Best Regards,

Fei Han

Leave a Reply