How to get match column names and unmatch column names from two data tables using linq

Hi Everyone,

I have two DataTables, DT1 and DT2(Column1, Column2, Column3, Column4, Column5) . I want to find the match and unmatch column values and put in different sheet . How can I achieve this using LINQ. As there are lot of data forloop takes lot of time to process. I want to achieve this using LINQ

For example ,if Column1 and Column3 is match it show give the column name and its values. If Column 4 and Column 5 unmatch it should give column name and column values.

Hi @marina.dutta

Can you please share sample input and expected output.

Regards

Hello @marina.dutta ,
Should all the Rows of a particulare column column match with another column, could u be more specific.
Regards

@Parvathy @Akshay_B

I have two excels DT1 and DT2 . I need to compare two excel column values and if the values dont match need to populate in another excel sheet the unmatch column values


Helli @marina.dutta ,
Correct me if I m worng, so you are comparing two datatable , based a certain column value, and if a particular row matched put that into lets say Sheet1 and if the do not match put that row into another sheet lets say Sheet2
Regards

@Akshay_B

yes and also if one column is not match get that column value

@Akshay_B

I got the two rows that are unmatched using query dt_pdfout.AsEnumerable().Except(dt_out_sales.AsEnumerable(),System.Data.DataRowComparer.Default).CopyToDataTable however My unmatched columns are Total Contract Value and Scheduling&Delivery . I am getting entire rows . Is it possible to get the only column values that are unmatched .

For example here in this case Total Contract Value and Scheduling&delivery is unmatched . I just want these two values

@marina.dutta

Please check

cheers

1 Like

@marina.dutta

Is this resolved?

cheers

@Anil_G

Yes Its resolved. Thank You so much

1 Like

@marina.dutta

glad.can you please close this

cheers

@Anil_G

Just one request. Can you explain the query what u did please ?

@Anil_G

Already marked it as solution .

@Anil_G

@marina.dutta

break down

For each row of dt1 select and load the data into dt3…
dt1.AsEnumerable.Select(function(x) dt3.Clone.LoadDataRow

First column to be quote number
({x(“Quote Number”).ToString,

From the matching row in dt2 with quote number in dt1…get the row as itemarray
String.Join(“,”,dt2.AsEnumerable.First(function(y) y(“Quote Number”).ToString.Equals(x(“Quote Number”).ToString)).ItemArray.

For each item in the array check if the value in dt2 and dt1 are matching then dont get column name else get the corresponding columnname and add it as second item or second column to dt3
Select(function(y,i) If(Not y.ToString.Equals(x(i).ToString),dt1.Columns(i).ColumnName,“”)).Cast(Of String).Where(function(z) Not STring.IsNullOrEmpty(z)))},False)).CopyToDataTable

hope this helps

cheers

2 Likes

@Anil_G

Thanks a lot.

1 Like

@Anil_G

There were two topics opened with same Questions. I have marked your solution and closed both of them. Let me know if its still open.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.