ENikS/LINQ

can't call methods on IGrouping

Closed this issue · 9 comments

I installed 2.4.32 in my package.json. This code compiles but fails at runtime. I tried calling Count() on the grouping and that didn't work either. This is from TypeScript, most recent VS Code, recent create-react-app.

test("create and group 1000 items", () =>
    expect(Linq
        .Range(1, 1000)
        .Select(i => i)
        .GroupBy(i => i)
        .Select(i => i.Where(j => true)) // error i.Where is not a function.
        .Count()).toBe(1000));

I'm new to GitHub and javascript. Help me out here. The code below works. Grouping produces something that subclasses Array (see below) so 'length' works but '.Count' and other linq operators do not. Not sure why intellisense thinks .Count should work.

test("linq group", () => {
    let x = Linq.from([1, 2, 2, 3, 4, 5]);
    let result = x
        .Where(i => i != 3)
        .GroupBy(i => i)
        .Select(i => (i as any).length)
        .Max();
    expect(result).toBe(2);
});

// you defined it this way
export interface IGrouping<K, R> extends Array<R> {
    key: K
}

Try it here. Does not work.

https://codesandbox.io/s/l97norvo27

ENikS commented

Grouping is nothing more than a regular array. In order to use it with all the LINQ methods you have to convert it into Enumerable:

const maxOfAnyItem = Linq.asEnumerable(numbers)
  .GroupBy(i => i)
  .Select(i => Linq.asEnumerable(i).Count())
  .Max();
ENikS commented

Do you have a solution in mind?

ENikS commented

Would you like to try it out and report on performance overhead? I am afraid it would make it much slower. This whole IGrouping interface is remnant from C# and serves no purpose in JavaScript.