1

I am trying to group my data by 2 properties, and sum two other properties for each group. My code is off just a bit, because I am getting the same sum for both the fields (value and quantity). What am I missing? Thanks!

The code -

var linq = Enumerable.from(data); var result = linq .groupBy( "{ pCo: $.portCo , sName: $.secName }", "$.value, $.quantity", "{ portCo: $.pCo, security: $.sName, value: $$.sum($.value), quantity: $$.sum($.quantity) }", "$.pCo + ' ' + $.secName") .toArray(); 

1 Answer 1

2

Ok, after n trials and (n-1) errors, got it to work with the following syntax:

var linq = Enumerable.from(data); var result = linq .groupBy( "{ pCo: $.portCo , sName: $.secName }", null, "{ portCo: $.pCo, security: $.sName, value: $$.sum('$.value'), quantity: $$.sum('$.quantity') }", "$.pCo + ' ' + $.secName") .toArray(); 

The rationale for the null is not clear to me, and i needed '$.x' quotes for the property names in the sum function.

Inspiration for the solution from Jeff's answer here - https://stackoverflow.com/a/15647792/2011729

Sign up to request clarification or add additional context in comments.

1 Comment

Passing in null when a function is expected will be replaced with the identity function. In this case, you just needed to produce an object that contained a value and quantity property. Your original wasn't a valid expression. Then as you've figured out, you needed to pass in a selector to the sum() function.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.