LINQ: Basics, But Still Pretty Advanced(Part-II)

This is Part-II of the same problem statement from Part-I.

We use tables a lot in UiPath, because it’s pretty darn convenient for housing data. There are other ways of preparing and storing data, but we won’t go into any of that in this article.

Let’s take it slow.

Food tastes better when you slow cook them, and I know you’re hungry from all that waiting, but you have to wait some more until I am done cooking, or you will suffer indigestion.

We will continue with the example from before.

Here are the DataTables once again:


We will be using a dictionary to fill DtResult with data.

Take Out Your Dictionary

There are times when you can leverage the Dictionary Variable.

This happens to be one of those “times”.

In case you don’t know what a Dictionary Variable is, it’s a collection of Key-Value pairs, where the key is always unique.

You may have:

{{“Fruit1”, “Apple”}, {“Fruit2”, “PineApple”}}

And you may also have:

{{“Fruit1”, “Apple”}, {“Fruit2”, “Apple”}}

But you can’t have:

{{“Fruit”, “Apple”}, {“Fruit”, “PineApple”}}

The Key has to be unique, otherwise it isn’t a dictionary.

Also, if you wish to declare and add items to a Dictionary in UiPath, here is how you will go about doing it:

New Dictionary(Of String, String) From  {{“Fruit1”, “Apple”}, {{“Fruit2”, “PineApple”}}

Who Is Worthy

Tell me, which DataTable would make a great Dictionary?

Is it Dt1 or Dt2?

Based on what criteria does data have to get pooled into Dt3?

Think about it for a while before reading on.

And The Answer Is…

NOT Dt1.

Dt1 only contains the Department ID, but there is no way to link it to with the Designation we need to fill Dt3 with.

Dt2 contains Department ID and Designation, so in a way it maps the Department ID with the Designation. They will make excellent Key-Value pairs.

The LINQ that will perform the conversion is as follows:

Dt2.AsEnumerable.ToDictionary(Function(k) k(0).ToString.Trim, Function(v) v(1).ToString.Trim)

DataTable -> List of DataRows ->First Column taken as Key, while Second Column Taken as Value

I hope that makes sense.

If not, then spend some time staring at it until you either get it, or get admitted into a mental asylum.

I’m two steps ahead of you.

Let Us Proceed

The dictionary we created earlier will help us populate data into Dt3 with ease.

The LINQ that will perform the data Addition is as follows:

(From d In Dt1.AsEnumerable
Let Name = d(“Name”).ToString.Trim
Let Designation = DictMaster(d(“Department ID”).ToString.Trim)
Let ra = New Object(){Name, Designation}
Select DtResult.Rows.Add(ra)).CopyToDataTable

Our Dictionary has been referenced on Line 3.

We retrieve values from dictionaries by passing in the “Key” which in this case is the d(“Department ID”) row item.

Department ID is the Key passed in, and Designation is the Value outputted.

Dictionary(Key) -> Value

DictMaster(d(“Department ID”).ToString.Trim)
//This gives us the Designation(Value) for that particular ID(Key)

If the syntax and keywords used in the above snippet leave you confused, head over to the first article in this series, and you will understand…slightly more than you currently do.

And there you have it.

The result will look something like this:

Bonus Round

What If Our Master Table Contains More Than Two Columns?

That can be resolved by grouping the columns into two items.

Tuples will help us with that projection, so let’s give it a try:

(From d In Dt1.AsEnumerable
Let KeyValue = Tuple.Create(d(“First Name”).ToString & d(“LastName”).ToString, d(“Department ID”).ToString)
Select KeyValue).ToDictionary(Function(k) k.Item1, Function(v) v.Item2)

Or you can directly select the projection like so:

(From d In Dt1.AsEnumerable
Select Tuple.Create(d(“First Name”).ToString & d(“Last name”).ToString, d(“Department ID”).ToString)).ToDictionary(Function(k) k.Item1, Function(v) v.Item2)

Play around with it some more, until you get a feel for it.

Here is what the Lambda Expression would look like:

Dt1.AsEnumerable.ToDictionary(Function(k) k(0).ToString.Trim & k(1).ToString.Trim, Function(v) v(2).ToString)

And with that, we conclude today’s session.

I know I rushed through the entire thing, that’s because we have covered the basics in previous articles.

If any of this confuses or frustrates you, head back over to your UiPath Studio and practice them some more.

You have to spend some time with it, get to know it a little bit, before you can show it off to your friends.

That right there is the mark of a professional developer. I am not saying you are incompetent, but don’t you dare look me in the eye when I walk past you.

Leave a Comment