Linq query help cast of ui list

Hi everyone,

Got a variable called UiList: A list witch contains several UI elements
Got another variable called Label: This variable is a string witch contains the aaname property of the ui element

I need to use a query to select/find/get the ui element witch aaname property is equal to the label variable

I’m trying to build something like: UiList.Cast(Of UiElement).Contains(Label) but this obviously does not work.

Any clues?

Thanks in advance

Hi @Pelayo_Celaya_Fernandez,

Follow the steps as below:

  1. Use a For each Activity. The argument type is System.Windows.UIElement
  2. inside of For each Activity (Body) you can include an If Activity and type the following condition:
uiElement.Get(aaname).ToString.Trim.ToLower.Equals(Label.ToString.Trim.ToLower)

Hope it helps you!

1 Like

UiList.Cast(Of UiElement)().FirstOrDefault(Function(ui) ui.Get(“aaname”).ToString() = Label)

This works for me.

It returns the relevant UiElement

1 Like

Hello,
Why did you try to cast your list if you said that it is a list of Ui Elements already?
You can do this directly in a LINQ query, without having to iterate through the list of UI elements:
UiList.AsEnumerable.Select(function (x) Label.Contains(x.Get(“aaname”))).ToList()

1 Like

I though this was the solution but it returns error:

error BC30518: Overload resolution failed because no accessible ‘FirstOrDefault’ can be called with these arguments:
Extension method ‘Public Function FirstOrDefault(defaultValue As UiElement) As UiElement’ defined in ‘Enumerable’: Lambda expression cannot be converted to ‘UiElement’ because ‘UiElement’ is not a delegate type.

This one returned error too. But I didnt though it would work for me since I want to return a unique UiElement from the List of UiElements and you end the query with *.ToList()

Thank you, this works but is not what Im looking for.

I mean I have no problem doing a for each of the list of uielements and checking if aaname equals to the variable label and then use a breack if I get a match.

I’m trying to find a more optimal way of retrieve that Ui Element.

Thank you,

we assume
UiList - a collection (IEnumerable, Array, List… of UiElement)
Label - a String Variable

Assign Activity:
myFoundUIElement | Datatype: UiElement =

UiList.Where(Function (x) x.Get("aaname").toString.Trim.ToUpper.Equals(Label.ToUpper)).FirstOrDefault()

And you can do all inspections / RnD / Prototypes within the immediate panel
Understanding the 6 Debugging Panels of UiPath in the easiest way possible! - News / Tutorials - UiPath Community Forum

2 Likes

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