UiPath: LINQ Is Not Everything

After learning LINQ, I started using it everywhere, even in situations that didn’t necessarily require it.

Developing and testing LINQ is time consuming (I’m still a rookie), so I ended up wasting precious time that could have been better utilized on other projects (or articles).

Don’t Get Me Wrong

LINQ can achieve most outcomes, but it isn’t a magic bullet.

You can cook omelets with a flatiron, but just because you can, doesn’t mean you should…unless you are staying at a college hostel, then I totally understand.

I’ve engaged in some of that shenanigans myself, and it is finger lickin’ nostalgic.

When you are tasked with identifying and performing operations based on patterns, you are better of using Regex.

But we aren’t going to use Regex here(At least not yet).

I am going to fry some omelets with my flatiron just to show you how complicated and nerve wracking it can get, as some of that eggy goodness breaks and starts oozing through the sides.

I didn’t want any of it to go to waste, so I licked it off the iron box.
Yes, I incurred 1st degree burns but it was worth it.


An application similar to excel, contains a column with multiple selection dropdowns. Each row contains a dropdown and you are provided with the option of checking one or more options.

When an API call is made to retrieve items from the application, the items are retrieved as a single block of string, where each individual items are separated by commas.

But here’s the problem.

The items themselves contain commas.

How will you split the string into individual items?

The options appear as drop downs, and are separated by fancy looking grey boxes once selected.
It gets boxed up into a single block of string separated by commas.

By Using The Split Method!

That won’t do, because sometimes the items themselves contain commas.


This snips off elements that ought to remain together, so that’s a no-no.

It’s in cases like these that I pull out my magic flatiron and start ironing away.

I could be trying to smooth the ripples in water with my flatiron for all I know, but I’m going to do it anyway.

Guess who I stole that from?

Before we proceed any further, it would make sense to introduce some nonsensical test data so that we may understand the problem, search for patterns, and come up with a solution.

Test Data:

“Tellurium-12 Liquid, Tellurium-15 Solid, Manganese-16 Solid, Manganese-11 Liquid,Solid, Tellurium-32 Gas,Solid”

I’ve enclosed them in square brackets to make it easier to identify:

[Tellurium-12 Liquid], [Tellurium-15 Solid], [Manganese-16 Solid], [Manganese-11 Liquid, Solid], [Tellurium-32 Gas, Solid]

Just for the heck of it, lets apply a split operation and see what happens.

StringVariable.Split({“,”c}, StringSplitOptions.RemoveEmptyEntries)

Can you guess what the output will look like? Your eyes probably might have already wandered below.

 { “Tellurium-12 Liquid", “ Tellurium-15 Solid”, “ Manganese-16 Solid”, “ Manganese-11 Liquid”, “Solid”, “ Tellurium-32 Gas”, “Solid” }

The highlighted elements which were supposed to be together, were split apart, and now they are single and ready to mingle.

We don’t want that, now do we? People ought to remain miserably married.

So let’s split them apart.

StringVariable _
.Split({“Tellurium”}, StringSplitOptions.RemoveEmptyEntries)

Which brings us to

{ “-12 Liquid, “, “-15 Solid, Manganese-16 Solid, Manganese-11 Liquid,Solid, “, “-32 Gas,Solid” }

The items have been split as expected, full of faults.

Let’s try to reintroduce Tellurium back into the family picture.

Str_Variable _
.Split({"Tellurium"}, StringSplitOptions.RemoveEmptyEntries) _
.Select(Function(s) _
If(s.ToString.Trim.StartsWith(“-”), “Tellurium”+s.ToString.Trim, s.ToString.Trim)).ToArray

Wonder what that will lead us into.

{ "Tellurium-12 Liquid, ", "Tellurium-15 Solid, Manganese-16 Solid, Manganese-11 Liquid,Solid, ", "Tellurium-32 Gas,Solid" }

But the Manganese family has decided to move in with the widowed spouses.

You see what the problem is?

There are so many operations involved, that it will make you go nuts.

Here is what the entire LINQ will look like:

Str_Variable.Split({"Tellurium"}, StringSplitOptions.RemoveEmptyEntries).Select(Function(s) If(s.ToString.Trim.StartsWith(“-”), “Tellurium”+s.ToString.Trim, s.ToString.Trim)).Select(Function(s) s.Split({"Manganese"}, StringSplitOptions.RemoveEmptyEntries)).SelectMany(Function(sm) sm).Select(Function(s) If(s.ToString.Trim.StartsWith(“-”), “Manganese”+s.ToString, s.ToString.Trim)).Select(Function(s) If(s.ToString.Trim.EndsWith(","),s.ToString.SubString(0,s.ToString.Trim.Length-1), s.ToString.Trim)).ToArray

Don’t believe me? Here, have another look.

You thought I was crazy didn’t you?
Now you know I’m insane.

And there you have it.

But it is ugly to look at and difficult to maintain.

What if they decide to add new categories which doesn’t start with the character we are splitting them on the basis of?

Mn-Te-43, or MT-50? What then? For Each? If? If what? What If I start crying?

There is no “If”.
I already am.

Using LINQ here is risky, since we end up having to hardcode few values.

Hardcoding is never a good idea. Developer are allergic to that hardcore stuff.

The problem with hardcoding values, is that it diminishes its reliability. Processes with rigid values make it rigid as well.

Any changes made to inputs will severely affect its performance.

It could even crash and burn, and invite the end user’s scorn.

So let’s start frying eggs in pans, as God intended.

Introduction To Regex

Regex is short for Regular Expressions, and believe me when I say this, there is nothing “regular” about these expressions.

Here is proof of that.

Convinced yet?

Regex is the salt and pepper to our omelette.

Without it, our automations become bland and tasteless.

All this talk about omelette has me feeling a little peckish.

Maybe I’ll fry one and make myself a sandwich after I’m done writing this article.

Back To Our Problem

Regex is in many instances, a life saver.

Imagine having to come up with a super complex segment of code that could potentially fill novels, only for it to perform a handful of transformations…kind of like the process I just guided you through.

That being said, I could write a book or two on this.

Once people purchase my non-existent book, I will slowly start amassing non-existent wealth and lead a carefree life! Of course none of that is going to happen, because I am going to write a book and lead a stressful life.

Let’s look at our problem through our regex tinted glasses.

The fog has finally lifted, and now everything is crystal clear!

What do you see?

All that green stuff is what we have captured using Regex.

I know what you are thinking, those are the items we don’t want, so why highlight those?

That’s because while regex offers you the ability to match patterns, coding offers you a multitude of operations to perform on the said pattern.

You can retrieve a single match, retrieve all matches, replace the matched pattern with another keyword or even Split the entire string on the basis of the matched pattern.

But simply splitting it on the basis of “(, )” won’t cut it, since it will take either comma or space, and not the two together.

It is a little tricky, but once you get a hang of it, it will all fall in place.

System.Text.RegularExpressions.Regex _
.Split(StringVariable, “[,\s]{2}”).ToArray

And there is your answer.

Couldn’t We Split It on the Basis of Comma Followed By Space?


Str_TestData _
.Split({", "}, StringSplitOptions.RemoveEmptyEntries)

This is a working solution, however there are times when none of those options work.

That’s when you have to flash the bat signal and hope the creepy bat suit wearing guy who popped out of nowhere, can help you with your problem.

I heard he is a multi-billionaire under disguise. I’m sure a few million dollars could resolve ANY problem.

[^The End]

I have an interesting idea that I will detail in a coming post once I achieve it. I’m also trying to achieve the same result using LINQ, but regex seems like the right option.

What is this interesting idea you ask?

Stay tuned.

Leave a Comment