Hello,
I have two DataTables:
dt1
dt2
In both of them is a column “Order Number”
I would like to take Each “Order Number” from dt1 and check if it exist in dt2
If exist-> take next “Order Numer”
If not exist → write comment in dt1 (Column “Comment’)” This order does not exist in dt2
How to do it?
rikulsilva
(Henrique Lima da Silva)
January 26, 2023, 12:30pm
2
Hello, @aleksandra.plichta5
You can use For Each Row in Datatable in Dt1 and put into if condition to search Order Number in dt 2 and use this expression:
dt2.asEnumerable.Where(Function (x) x.item(“Order Number”).ToString.ToUpper.Trim.Equals(curretRow(“Order Number”).ToString.ToUpper.Trim)).Count = 0
Basically, get Order Number and lookup in dt2. If Order Number not found in any row in dt2, Count return = 0.
Then in True branch, put assign activity to currentRow.item(“Comment”) = “This order does not exist in dt2”
Yoichi
(Yoichi)
January 26, 2023, 2:08pm
3
Hi,
FYI, another approach: using LINQ with Invoke code.
dt1.AsEnumerable.Where(Function(r) dt2.AsEnumerable.All(Function(r2) r2("Order Number").ToString<>r("Order Number").ToString)).ToList().ForEach(
Sub(r)
r("Comment")="This order does not exist in dt2"
End Sub
)
Sample20230126-1aL.zip (2.9 KB)
Regards,
ppr
(Peter Preuss)
January 26, 2023, 2:20pm
4
rikulsilva:
Count return = 0.
Let us mention this recommendation as well:
So we rewrite to any with predicate
…Any(Function (x) …)
Or
…Where(Function (x)…).Any()
In case of another optimization is needed also have a look here:
Let us introduce the next changes
[grafik]
we are creating lists with the concatenated strings from dtold, dtnew
and creating a dictionary of only the items common in both lists
dtNewSet.asEnumerable.Select(Function (x) String.Join(“#”, arrColSet.Select(Function (k) x(k).toString.ToLower.Trim))).Distinct().toList
dtOldSet.asEnumerable.Select(Function (x) String.Join(“#”, arrColSet.Select(Function (k) x(k).toString.ToLower.Trim))).Distinct().toList
ListDT1.Intersect(ListDT2).ToDictionary(Fun…
we took benefits from lists/dictionary keys
1 Like
rikulsilva
(Henrique Lima da Silva)
January 26, 2023, 2:29pm
5
Very thanks for advice.
I took notice and I will sharing in another oportunities