Getting confuse when to use which on List Methods

Hi All,

Am trying to understand how to use code, so getting confuse
Step1 . I have used List with initalization as New List(Of Int32) from {1,4,6,3,7,2,4,9,6}, assigned to list1 variable which is of type System.collection.generic.List

Now I want to find Min as example,
so I have written list1.Min and getting expected value.
But If I try use Invoke Method assigning as Target object list1 and MethodName as Min, getting an error.

image

Same thing I have tried for other methods for some of Invoke method is working and for some of in assign it is working…Need some clarification

Thank you…

Hi @Sharanabasava

The error you are encountering happens because Min is an extension method provided by the System.Linq namespace and not a direct method of the List<T> class. To use such methods in UiPath, you need to use the Invoke Code activity instead of the Invoke Method activity.

The syntax used within Assign activity is the right one to use list1.Min.

Regards

@Sharanabasava

You can use the below code in Invoke Code to or Assign activity:

WAY 1:
=> Assign Activity → list1 = New List(Of Int32) from {1, 4, 6, 3, 7, 2, 4, 9, 6}
list1 is of DataType System.Collections.Generic.List(System.Int32)

=> Assign Activity → minValue = list1.Min

=> Message Box → minValue

WAY 2:
=> Assign Activity → list1 = New List(Of Int32) from {1, 4, 6, 3, 7, 2, 4, 9, 6}
list1 is of DataType System.Collections.Generic.List(System.Int32)

=> Use below code in Invoke Code:
minValue = list1.Min()
Invoked Arguments:

=> Message Box → minValue

Hope you understand!!
Regards

@Sharanabasava

Previous question I got clarification, Thank you.
I have one more doute
I have list need to find distinct, so I have used list1.Distinct().Tolist and got the result.

But why to choose .ToList and when we need to choose.

Hi @Sharanabasava

The purpose of list1.Distinct().ToList() is to create a new list that contains only the unique elements from the original list1, without any duplicates.

Hope you understand!!
Regards

Last question…
for adding items in the list, we will use Invoke method.
we cannot use assign and add the items. without converting into String and Concat or without using linq

Hi @Sharanabasava

You’re right.

We can use Invoke Method to add items to a list or there is another way too.

=> Initialize the List as New List(Of Int32)
=> Run a For Each loop and you can add items to list using Append Items to Collections.

Below is an example:


Output:

It is based on the conditions, how we use and assign the syntaxes.

Hope you understand!!
Regards

Thank you for the clarification of my queries

1 Like

You’re welcome @Sharanabasava

I’m happy to assist you.

Happy Automation!!

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