How to use LINQ to compare two DataTable values and return a boolean

Hi all,

I would like to know how can I use LINQ to compare two DataTable values and return boolean if both values are int the data tables?
For example
DT1 has value G25685 in Column(“Shipment”)
And
DT2 has G25685 in Column(“Shipment”)
return True if so or False if not

Thank you for all the help in advance.

Hi,

Do you mean you want to check if some same value exists in specific column of both datatable?
If so, the following will work.

image

dt1.AsEnumerable.Select(Function(r) r("Shipment").ToString).Intersect(dt2.AsEnumerable.Select(Function(r) r("Shipment").ToString)).Any

Sample20220910-1.zip (2.6 KB)

Regards,

Hi, thank you, that worked, how could I check if a single data table contains a specific value, let’s say I want to check if DT1 has the value G05468, and if so return a boolean?

Hi,

How about the following?

dt1.AsEnumerable.Any(Function(r) r("Shipment").ToString="G05468")

Regards,

1 Like

Yes this works perfectly, so I have one last question, if the function Contains exist, why can I not use it for datatables?

Hi,

Can you elaborate? Do you mean String.Contains or IEnumerable.Contains?

Regards,

Hi,
I mean IEnumerable.Contains

Hi,

If we need to use IEnumerable.Contain for the above latter expression, it will be as the following.

dt1.AsEnumerable.Select(Function(r) r("Shipment").ToString).Contains("G05468")

Regards,

Yes, this worked perfectly as well, thank you so much @Yoichi

1 Like

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