ENikS/LINQ

[Question] group by multiple parameters

Closed this issue · 1 comments

I have an easy christmas question for you :-)

In C# linq it seems easy but I don`t get it with TypeScript.

How can I group by 2 parameters like schoolclassNumber AND SubjectName?

Normally I would ask that on SO, but there is no linq-es2015 tag existing ;-)

UPDATE

I was confused and wanted actually something different. Of course I could just make a getter for the schoolclassNumber and subjectName together.

Thus my original question is how can I pass the TestType.id property as property of the returned item object?

export class TestType {

    constructor(obj)
    {
        this.id = obj.id;
        this.name = obj.name;
        this.schoolclassNumber = obj.schoolclassNumber;
        this.subjectName = obj.subjectName
    }

    id: number;
    name: string;
    schoolclassNumber: string;
    subjectName: string;
}
   let groupedTestTypes = Enumerable.from(testTypes)
         .GroupBy(g => g.schoolclassNumber, g => g.name, (key, names) => { return { key: key, names: names } })
        .Select((item, index) => new TestTypePair(item.key, Enumerable.from(item.names).ToArray())) 
         .ToArray();

    for (var x of groupedTestTypes) {
      console.log(x.key);
      console.log("------------------");
      for (var z of x.names) {
        console.log(z);
      }
    }
```

Ok finally got it :-)

let groupedTestTypes = Enumerable.from(testTypes)
         .GroupBy(g => g.schoolclassCode, g => g, (key, g) => { return { key: key, testTypes: g } })
        .Select((item, index) => new TestTypePair(item.key, Enumerable.from(item.testTypes).ToArray())) // FIX: warning
         .ToArray();

    for (var x of groupedTestTypes) {
      console.log(x.key);
      console.log("------------------");
      for (var z of x.testTypes) {
        
        console.log(z.id);
        console.log(z.name);
      }
    }