Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
If I want to compare two datatables and get the difference in new datatable but I want to keep an uncompared column.
example:

first Datatable
Name | Number
---- |-------
Jude | 12
Mark | 14
Bin | 15

second Datatable
Name
------
Jude
Robin
Kamil

the Datatable must have:
Name | Number
-------|----------
Mark | 14
Bin | 15
Posted

1 solution

ur sample data

C#
DataTable dt1 = new DataTable();
dt1.Columns.Add("Name",typeof(string));
dt1.Columns.Add("Number",typeof(int));
dt1.Rows.Add("Jude", "12");
dt1.Rows.Add("Mark", "14");
dt1.Rows.Add("Bin", "15");
DataTable dt2 = new DataTable();
dt2.Columns.Add("Name", typeof(string));
dt2.Rows.Add("Jude");
dt2.Rows.Add("Robin");
dt2.Rows.Add("Kamil");


using LINQ the unmatched 'Name' fields of two tables

C#
var filter= from firstDt in dt1.AsEnumerable()
            where !(from secondDt in dt2.AsEnumerable() select             secondDt["Name"]).Contains(firstDt["Name"].ToString())
                     select firstDt;
         DataTable resultDt = filter.CopyToDataTable();


resultDt(Output)

good luck ;-)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900