Compare 2 columns in a single excel sheet

Hi All…
I have to compare 2 columns (the 2 columns are in date formate) in a single excel sheet. if the data is match i have write it to another excel file, if it is not matched that data also have to be stored in another excel file.

TIA! :slight_smile:

Hello @soundarya.a

Matching Records

dt.AsEnumerable().Where(Function(x) x("Col1").ToString.Equals(x("Col2").ToString)).CopyToDataTable

Non Matching Records

dt.AsEnumerable().Where(Function(x) Not x("Col1").ToString.Equals(x("Col2").ToString)).CopyToDataTable

Thanks,
Sanjit

Hi @soundarya.a ,

Just a slight modification over @Sanjit_Pal 's code:

dt.AsEnumerable().Where(Function(w) CDate(w(0).ToString).Equals(CDate(w(1).ToString))).ToList()

We will convert it to a List of DataRows before copying it to a Datatable because if the operation returns null values i.e., none match the criteria, then the .CopyToDatatable() will throw an error.

Its best to check if it generates any results before going ahead and adding it to a Datatable.
Similarly:

dt.AsEnumerable().Where(Function(w) Not CDate(w(0).ToString).Equals(CDate(w(1).ToString))).ToList()

Kind Regards,
Ashwin A.K

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