2

I currently have an array of objects such as

[{Test: "123", Two: "234"}, {Test: "123", Two: "234"}, {Test: "123", Two: "222"}]

Using linq.js how would I be able to run a group by and return a list that returns the count of the duplicate (test,two) values. I want my output to be something like:

[{Key: {Test: "123", Two: "234"}, Value:2}, {Key: {Test: "123", Two: "222"}, Value:1}  ]

I am thinking the GroupBy method in Linq.js might help however I am not sure how to use it.

Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272
nav
  • 509
  • 4
  • 19

1 Answers1

0

You'll need to use the overload of GroupBy() that includes a compare selector. Your keys are the objects but you need to provide a way to compare them. Project the item to something that can be compared (e.g., a string of the keys).

var data = [
    { Test: "123", Two: "234" },
    { Test: "123", Two: "234" },
    { Test: "123", Two: "222" }
];
var query = Enumerable.From(data)
    // GroupBy (keySelector, elementSelector, resultSelector, compareSelector)
    .GroupBy(
        null, // (identity)
        null, // (identity)
        "{ Key: $, Value: $$.Count() }",
        "'' + $.Test + '-' + $.Two"
    )
    .ToArray();
Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272