LINQ (Group By Sum) Query Result To DataTable using CopyToDataTable


I’m using studio 20.4 and want to do ‘group by sum’ operation on a DataTable like belows.
To do this job, I wrote a ‘Invoke code’ running LINQ Query and CopyToDataTable operation.
However, my ‘Invoke code’ throws runtime exception. (Please refer to the attachment)

Could anyone please figure out this issue? Thanks in advance!

[From DataTable] dtSource

BizCode | Price | Tax
AAA     |   100 |  10
BBB     |   200 |  20
CCC     |   300 |  30
BBB     |   400 |   0
CCC     |   500 |   0

[To DataTable] dtTarget - Group By BizCode and then Sum(Price) & Sum(Tax)

BizCode | Price | Tax
AAA     |   100 |  10
BBB     |   600 |  20
CCC     |   800 |  30

[Test] LINQ2DataTable.xaml (10.7 KB)

Hello @yhkim7
your code is almost complete, you just missed to instantiate the dtTemp


you can also Try using linq like
DtRecrod.AsEnumerable().GroupBy(Function(row) row.Item(“BizCode”)).Select(Function(group) New With {.BizCode= group.Key, .Price= group.Sum(Function( r ) Double.Parse(r.Item(“Price”).ToString()))})

Thanks for your correction.
Yes, you’re right. I missed that instantiation part! because I get used to use ‘Build Data Table’ Activity at which I can simply defined table and then set columns and put data. :slight_smile:

Thank you~

1 Like

I also know the ‘Method Syntax’ in LINQ, but the point is that I wanted to get the result into DataTable using CopyToDataTable not into IEnumerable(Of Object) with which I can’t put into DataTable easily.
Thanks for you reply.

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