Group by query in datatable

Hi i have a dataTable, which has say 10 cols, named partname,qty, cost etc i would like to group by partname… how to convert this query into uipath style ?

select partname,sum(qty) from table group by partname

Any help would be appreciated.

do you want to select that particular partname and sum up the respective rows?

1 Like

Step 1: Getting list of unique partnames-

partname_list = (from row in table.asEnnumerable select Convert.ToString(row.item(partname)).toList.Distinct

Step 2: Summing up all the distinct part names -

for each partName in partName_list:
	sum_partName = (from row in table.asEnnumerable where row.item("PartName").toString.Equals(partName) select convert.int32(row.item(qty))).Sum(Function(x) Convert.ToDouble(x))

Use partName and sum_partName here to add a row to your Datatable using add row activity

i want the sum of quantities partname wise

@hss
Give a try on

(From d In yourdatatableVar.AsEnumerable
Group d By pn=d(“partname”).toString.Trim Into grp=Group
Select New Tuple(Of String, Int32) (pn, grp.Sum(Function (x) CInt(x(“qty”).toString.Trim)))).toList

this wil return a list of Tuple(Of String, Int32) that you can iterate. With yourSingleTupleVar.Item1 you retrieve the groupname, with yourSingleTupleVar.Item2 you retrieve the sum

Sharing some sample data with us would help us to work out demo xaml for you. With a prepared datatable the list of tuples can easy be added to the datatable

4 Likes

@ppr thank you. This is giving me the desired result.

@hss
ensure the TypeArgument is set to Tuple(Of String, Int32) for the for each activity
then in your case you iterate over each tuple (in your case with varname row) and can access with row.item1 or row.item2.toString

Yes @ppr i figured that out after i posted this query. Sorry bout that :slight_smile:
Thank you for the solution.

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