JavaScript Linq.Js Group By with Count linq group with join,linq group with count,linq group

I have the following array:

var data=  [{ "Id": 1, "Name": "NameOne"}
            { "Id": 2, "Name": "NameTwo"}
            { "Id": 2, "Name": "NameTwo"}]
            { "Id": 3, "Name": "NameThree"}]

Using linq.js I would like to return the following array:

var data=  [{ "Id": 1, "Name": "NameOne", Total: 1}
            { "Id": 2, "Name": "NameTwo", Total: 2}
            { "Id": 3, "Name": "NameThree", Total: 1}]

This means that I need to use GroupBy() with a Count(). I am not sure how to apply this using the linq.js reference.

Answer:1

It's simple really:

var data = [
    { Id: 1, Name: 'NameOne' },
    { Id: 2, Name: 'NameTwo' },
    { Id: 2, Name: 'NameTwo' },
    { Id: 3, Name: 'NameThree' }
];
var query = Enumerable.From(data)
    // GroupBy (keySelector, elementSelector, resultSelector, compareSelector)
    .GroupBy(
        null, // (identity)
        null, // (identity)
        "{ Id: $.Id, Name: $.Name, Total: $$.Count() }",
        "'' + $.Id + '-' + $.Name"
    )
    .ToArray();

Use the overload of GroupBy() that includes the resultSelector, you'll want to grab the count of the grouped items (the second parameter).

Answer:2

I just discovered a weird issue with Angular 1.2.1, demonstrated in this fiddle (tested in both IE, FF, and Chrome): if I create a very simple templated directive, it fails to work as <my-element /&...

I just discovered a weird issue with Angular 1.2.1, demonstrated in this fiddle (tested in both IE, FF, and Chrome): if I create a very simple templated directive, it fails to work as <my-element /&...

I have 2 long URLs that point to the same page, http://preview.tbwabox.co.nz/_v005/index.html#buying-a-car http://preview.tbwabox.co.nz/_v005/index.html#buying-a-house and I would like to check if ...

I have 2 long URLs that point to the same page, http://preview.tbwabox.co.nz/_v005/index.html#buying-a-car http://preview.tbwabox.co.nz/_v005/index.html#buying-a-house and I would like to check if ...

I have a webapp that makes calls to a database. Every once in a while, we get a javascript alert dialog that says that the transaction was deadlocked. Honestly, we don't care if that happens because ...

I have a webapp that makes calls to a database. Every once in a while, we get a javascript alert dialog that says that the transaction was deadlocked. Honestly, we don't care if that happens because ...

  1. javascript alert block execution
  2. javascript alert block
  3. javascript block alert box

I'm trying to use formdata object to send form data to my server. I need this because one of my input fields is a file. However the formdata object is blank when I try to send the data to my server, ...

I'm trying to use formdata object to send form data to my server. I need this because one of my input fields is a file. However the formdata object is blank when I try to send the data to my server, ...

  1. formdata object empty
  2. form data empty object
  3. javascript formdata object empty
  4. formdata append empty object
  5. formdata returns empty object