Compare two columns from different workbooks with linq

Hello everyone, could you help me with something, I must compare two columns of two different workbooks.
I have workbook A and workbook B, I need to obtain the elements of the column “Code Materials” (the column is called the same in both files) that are in workook B but not in A

Good afternoon @Juan_Esteban_Valencia !

you can use the following LINQ expression to accomplish what you need:
Example screenshots:
WorkbookA:
image

WorkbookB:
image

By using the following expression, this is the output received:
image

Expression:

var_WorkbookB.AsEnumerable.Where(Function(x) Not var_WorkbookA.AsEnumerable.Select(Function(y) System.Text.RegularExpressions.Regex.Replace(y(0).ToString,"[^a-zA-Z]", "").ToLower).ToList.Contains(x(0).ToString)).CopyToDataTable

Where var_WorkbookB is the DT containing workbook B, and var_WorkbookA is the DT containing workbook A.

Hope this is what you need!

Best Regards,
Ignasi

Thank you very much for the answer, but this line would work if the two tables have the same structure, right?
What happens is that the only thing that the two tables have in common is the column “Code Materials”

@Juan_Esteban_Valencia Got it, I did not quite get it the 1st time :slight_smile:

Prepared a slightly different solution then, the output will be an array, with the “Code Materials” that are in Workbook B, but not in Workbook A

  • Considering also the case of having different structures, and sizes

Workbook A:
image

Workbook B:
image

By filtering the DT’s only based on Code Materials Colum, you can kind of compare them as if they were arrays (In my case, comparing it as strings):

var_WorkbookB.AsEnumerable.Where(
	Function(x) Not (var_WorkbookA.AsEnumerable.Select(
		Function(y) y("Code Materials").ToString).ToArray.Contains(x("Code Materials").ToString)
	)
).Select(Function(z) z("Code Materials").ToString).ToArray

The output with that 2 previous simulated entries would be the values in Code Materials B, that do not exist in Code Materials A:

image

Hope now it is what you need! If not, can you send an example of WBA, WBB, and expected output?

Cheers!

1 Like

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