How to Select from within Array of Dictionary

Hi!
I have on array of dictionaries. I need to select one from the array, based on certain dictionary value.
E.g.
array contains [dict1,dict2]
dict1 contains “id”:“123”, “somejsoncontent”:“{xxxx…}”
dict2 contains “id”:“456”, “somejsoncontent”:“{yyyy…}”

I need to assign to a dictionary variable one of the dictionaries, dict1 or dict2, based on the id.

Tried to write some Select functions, but not able to get it fully working.
array.Select(Function(x) x.item(“id”).Equals(“123”)) returns Enumerable.WhereSelectArrayIterator<Dictionary<string, object>, bool> { true, false }
but it is not quite there yet :slight_smile:

Toni

arrFiltered =

YourArrayVar.Select(Function(x) x.item("id").Equals("123")).ToArray

will return all Dictionaries which are matching the filter

Hey @toni.korhonen
try to use:
selectedDict = yourArray.Where(Function(dict) dict("id").ToString.Equals("123")).FirstOrDefault()

Hi @toni.korhonen

Try this syntax:

selectedDict= array.FirstOrDefault(Function(x) x.ContainsKey("id") AndAlso x("id") = "123")

Hope it helps!!

1 Like

in addition to above

[HowTo] LINQ (VB.Net) Learning Catalogue - Help / Something Else - UiPath Community Forum

we keep in mind that a where Operators can have an result of 0,1 or many items
Operators like First, Last will return the defined element or will throw an exception
the FirstOrDefault, LastOrDefault will return the DataType default value, when return is empty. In your case it would return Null / Nothing as Default Value

1 Like

Thanks @Parvathy @ppr @pikorpa
Got it working. Studying LINQ never ends…

1 Like

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