Expresion with startsWith operator in Switch

Hello, I have a problem that I can’t find a solution for at the moment.
I’m using a for each to iterate through a data table whose source is an excel file.
I have a column with a code in which I have to process the record of that cell depending on the number with which the code starts, this depends on whether it starts with a succession of 4 numbers, with a different one of 3 numbers and with another more different one of 2 numbers.
My idea was to use a switch using the startswith operator, but it doesn’t work for me as a switch expression.
At the moment I’ve solved it using elseIf, but I’d like to know how to build the flow using switch:
expression> stringVar.StartsWith
case 1> 1234
case 2> 356
case 3> 93

Even though they are numbers in the variable they are strings

switch is expecting values and not statements for the case definitions
In such case we would implement a mapper logic and bring the values / ranges to discrete switch values

The “Switch” activity does not allow expression in case (VB “select case” allows this).
So the option is to use InvokeCode “helper” where you could use the power of “select case” to determine the case.

Similar case here:

Cheers

Change the type argument of switch to Boolean and let us know if it works or not

@Antonio_Perez

Ok, thanks.
Definitely, for a situation like the one I’m discussing, it’s much more readable and clean to use elseIF

Alternatively you could use following switch expression to replace the invoke code:

{93,356,1234}.Select(Function(num, index) New With {num, index}).
Where(Function(x) x.num < myValue).
Select(Function(x) x.index+1).
LastOrDefault()

The array {93,356,1234} defines boundaries of cases and should be in ascending order.

Cheers

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