I am working on a scenario where I have two excel files with two columns namely Column A and Column B. I am writing linq query to match the row of both the excel files. So far I have written this, but its not working fine. For one excel i am looping it by using for each data row. search =row1(“ColumnA”).ToString.Trim+" “+ row1(“Column B”).ToString.Trim
after that results= dt.Tables(0).Select(“ColumnA++Column B ='”+search+”'").FirstOrDefault()
where dt is for the second excel file.
your help in this regard will be highly appreciated
Not so sure on what is the exact output required, If you could provide us with Sample Input Data and the Expected Output for it, we could maybe provide you with appropriate suggestions.
However, check the below posts on retrieving the common rows :
outer is for each data row… looping on dt1 , storing the the values in search variable search =row1(“ColumnA”).ToString.Trim+" “+ row1(“Column B”).ToString.Trim
after this results = dt.Tables(0) .Select(“ColumnA++Column B =‘”+search +”’").FirstOrDefault()
the values from second datatable dt is stored in results
after that if condition is used to check whether results has some value matched or not like this “result is Nothing”, and then values are updating in excel accordingly
We are still confused on this implementation, and what are the types of dt , is it a DataSet type ?
The query was more towards whether you could provide us with the Input Excel files that you were working on. And also maybe provide what is the Expected Output for that inputs.
Alternatively to what @Gayathri_Mk proposed you could use following LINQ query
dt3 = dt2.clone
dt3 = (From a In dt1.AsEnumerable
Join b In dt2.AsEnumerable
On a("Column A") Equals b("Column A") And a("Column B") Equals b("Column B")
Select dt3.LoadDataRow(b.ItemArray, False)
).CopyToDataTable