Create percentage using LINQ query

Hi,

I have this table

image

I am trying to add the percentage using this query

{"%","",(100*dtResult.AsEnumerable.Select(Function(r) CInt(r(1))).Take(dtResult.Rows.Count-1).Sum/CInt(dtResult.Rows(dtResult.Rows.Count-1)(1))).ToString+"%"}

but I get this error.

Add Data Row: Input array is longer than the number of columns in this table.",

can anyone help me?

Thank you

@coder Try this one:

Dim newRow As DataRow = dtResult.NewRow()
newRow(0) = β€œ%”
newRow(1) = β€œβ€
newRow(2) = (100 * dtResult.AsEnumerable().Select(Function(r) CInt(r(1))).Take(dtResult.Rows.Count - 1).Sum() / CInt(dtResult.Rows(dtResult.Rows.Count - 1)(1))).ToString() + β€œ%”
dtResult.Rows.Add(newRow)

Hi @coder ,

I suspect from the Input Datatable, you have only two columns in place. So you would require to add only two values but you’re adding an Extra Space as well, maybe delete it and check.

{"%",(100*dtResult.AsEnumerable.Select(Function(r) CInt(r(1))).Take(dtResult.Rows.Count-1).Sum/CInt(dtResult.Rows(dtResult.Rows.Count-1)(1))).ToString+"%"}

Thank you,

i want the result like this output

image

@coder ,

Could you show us a Screenshot of what is the Current Workflow ?

It seems you would want to add only the Last Column Values. For such case, you could add a Data Column using Add Data Column activity and loop through the Datatable rows using For Each Row activity and update the column value accordingly.

yes, I want to add only the last column. I am using

Add Data Row

image

@supermanPunch

Thank you , but i get some error

@coder ,

Could you try adding the below activities after Group By Assign, Remove the Add Data Row activities and add the below :

Expression used in Assign :

((CInt(CurrentRow(1).ToString)/CInt(dtResult.AsEnumerable.Last.Item(1).ToString))*100).ToString+"%"

I am getting this result

image

@coder ,

Debug Visuals Before/After the For Each Update :
image

its working , thank you

1 Like

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