Is it possible to for loop without using foreach and decision statements?is there any way to write that one using assign?

is it possible to for loop without using foreach and decision statements?is there any way to write that one using assign?

Not with Assign, as the loop itself doesn’t produce or store a value, thus can’t be used as either side of it.

Closest to a For loop is a While loop with a counter as the condition.
for (i = 0; i < something.Length; i++) { .... }
becomes:
i = 0; while (i < something.Length) { // some logic i++; }

i kno this one.but actually i need usinng assign activity

I was doing some research this morning with List.ForEach but unsuccessful with Invoke Method as well.

For eg : dt.AsEnumerable().ToList.ForEach(Function(x) list.Add(x)) ( adding dt rows to list)

This is a bad example, as the list it self is created(ToList), but was trying to explore more options.

Right, you can use a For in a few ways within an Assign activity like what vvaidya is posting above.

I’m not an expert though. You can do something like this:
(From line In text.Split(vblf(0)) where line.StartsWith(“ABC”))

My example, takes a list of text and stores only certain lines to an array that meet a criteria.

@harisankarreddy
I fail to see what you want to assign a loop to. Can you elaborate?

@vvaidya
Function needs to return something, yours doesn’t. It would need to be a .ForEach(Sub(x) list.Add(x)), but unffortunately Statement lambda's cannot be converted to expression trees., so I don’t know how to pass an Action<T> as a parameter to InvokeMethod.
Last time I just created an activity that would do what I wanted instead.

@ClaytonM
That’s not a For loop, it’s a LINQ expression in query syntax :slight_smile: Still, it returns a value so can be used in assign perfectly fine.

lol, I know but for Assign you need to return a value so figured that’s kind of what he’s looking for. :stuck_out_tongue:

I tried Action parameter as well, still gives me same message. Will rest the case partially now and re-try when I need

Curious to know if you found a way out… Please let me know @ClaytonM @vvaidya

1 Like

Hi @rshanthi
Can you give an example of what you are trying to do?

There are ways to simplify code and avoid cluttered activities, however for loops you will need to use a For each activity, Invoke Code activity, Invoke VBA, or calling other scripting (Python, Powershell, vbscript). The only time you can skip using a loop like that is when the result will be a value, which you can use in an assign activity, such as lambda/linq expressions. Note: this topic was posted back in 2017, and For each activities use up less memory and process quicker now.

Hi @ClaytonM,

Have been told that we need to use lambda expressions instead of foreach for better performance. I opted for invoke code, however, this again will have poor performance as it is compiled at runtime. Hence, was experimenting with invoke method, and that was when I came across this page.

My requirement is to update a datatable with some code like this
inoutDt.AsEnumerable.ToList.ForEach(Sub(x) x(“Name2”) = x(“Name1”).ToString)

Let me know your thoughts.

Thanks,
Shanthi R

In version 2018 and later, performance with For Each loops are not much of a concern. I run tables with well over 10k rows to update multiple columns and only takes half a second. I do find lambda expressions useful though, but could never get the ForEach() working since it doesn’t produce a value - I was trying to do the same thing because older versions processed the loops extremely slow.

But, if you do get it working in like a Invoke Code or Method, let me know your results. I just don’t know if you really need to now though.

Regards.

Hello @rshanthi below might able to help you out…

You can use below expration inside the invoke code activity:
iodtSheet1.AsEnumerable.Where(Function(row) row(“ColumnName”).ToString.Trim.Equals(“Value”)).ToList().ForEach(Sub(row) row(“ColumnName”) = “Value”)

Hi @VISHAL_JAISWAL1 ,

I’m curious. is it possible to update multiple columns using a ForEach inside of an Invoke Code?

If so, I’d appreciate it if you could share the syntax.
I can open a separate thread for this if required, please let me know.

Kind Regards,
Ashwin A.K