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.
Yoichi
(Yoichi)
September 10, 2022, 2:18am
2
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.
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?
Yoichi
(Yoichi)
September 10, 2022, 3:05am
4
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?
Yoichi
(Yoichi)
September 10, 2022, 4:56am
7
Hi,
Can you elaborate? Do you mean String.Contains or IEnumerable.Contains?
Regards,
Yoichi:
IEnumerable.Contains
Hi,
I mean IEnumerable.Contains
Yoichi
(Yoichi)
September 11, 2022, 2:59am
9
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
system
(system)
Closed
September 14, 2022, 4:43pm
11
This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.