Muliple GroupBy

Jul 16, 2012 at 6:48 AM

Hi,

Firstly, excellent product!

I'm trying to group an array of json objects by 2 fields then sum another but am not having much luck with the syntax.

The data looks something like: 

var TableArray = [ {BtmBucket:5, SizeBucket:1, Weight:5.2}, {BtmBucket:5, SizeBucket:1, Weight:3.9}, {.....}  ]

The code I've got so far looks like:

var AggBucketData = Enumerable.From( TableArray ) .GroupBy( "{Btm:$.BtmBucket, Size:$.SizeBucket}", "$.Weight", "key,gp=>{btm:key.Btm, size:key.Size, Weight:gp.Sum()}" )                 .ToArray();

Any help would be much appreciated. Thanks!

Nick

Coordinator
Jul 19, 2012 at 7:57 AM

all JavaScript's object is not equal.

var _ = { A: 3 } == { A: 3 }; // false

so the add compareSelector for GroupBy.

var AggBucketData = Enumerable.From(TableArray)
    .GroupBy("{Btm:$.BtmBucket, Size:$.SizeBucket}", "$.Weight", "key,gp=>{btm:key.Btm, size:key.Size, Weight:gp.Sum()}",
        function (x) { return x.Btm + ':' + x.Size } // Add compareSelector
    )
    .ToArray();