Use of Anonymous types in LinQ query in UiPath


#1

I am reading from an excel file. Taking output in a datatable.
Now I want to use grouping LINQ query on this data table to get average of a column based on grouping done over other column.
Suppose column A contains account numbers and column “C” contains Units purchased. so my output will be average of units purchased grouped by account numbers. I want this output to be exported in another datatable.
How can I do that?
I am attaching my XAML in my reply below


#2

Main.xaml (10.4 KB)


#3

Hi @abskulkarni,

I have attached your example with slight changes. Please check it and confirm.

Main.xaml (11.0 KB)

You may also try it:
image

Assign iemResult to:

(From j As Object In DT.Rows _
	Group By x = New With {Key .AccID = DirectCast(j, DataRow)("Account ID")} Into g = Group _
	Select dtResult.LoadDataRow(New Object() {x.AccID, g.Average(Function(r) Convert.ToInt32(DirectCast(r, DataRow)("Units")))},False) _
)

#4

Hi @acaciomelo
Many thanks to give a direction. However, it is giving error in loading Datarow into dtResult. The thing is, Linq query is able to give IEnumerable result of Object. But, that object needs to be cast into {string, Double} anonymous type to get loaded into dtResult. While doing that, it is giving below error in “Invoke Method” step. Below is the error.


#5

Hi @acaciomelo
I have resolved it by writing some custom VB.NET code to parse IEnumerable(Object) result got from Linq query. I use this custom code in “Invoke Code” activity.
But thanks for giving me a direction. Also please let me know if there is an easy and out-of-box way to accomplish this.