1

Been trying to learn LinQJS from some while now by converting some old Linq-querys to LinqJs-query.

This is the Linq-query.

(from y in class1
 join x in class2 on y.Id equals x.Name
 group y by new { y.Date, x.Number } into xy
 select new class3()
 {
 }).ToList();

This my current attempt on it(who's been rewritten many times). I think I just dont really understand the syntax.

var example = Enumerable.from(this.class1)
    .join(
        this.class2,
        "y => y.Id",
        "x => x.Name",
        " "
    )
    .groupBy("x.Date", "y.Number")
    .select(xy= new Class3(), { })
    .ToArray();
Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272

2 Answers2

0

Well, you could do something like this

First, the join part.

var res = Enumerable.From(class1)
         .Join(
                class2,
                "x => x.Id",
                "y => y.Name",
                //I flattened all to make things more readable, you could also choose (x, y) => {c1:x, c2:y} for example
                "(x, y) => {dte:x.Date, id:x.Id, name:y.Name, nb:y.Number, val:y.val} "

                ).ToArray();

Then the group by part (you can of course do all in one)

        var res2 = Enumerable.From(res)
  .GroupBy("p => {Date:p.dte, Number:p.nb}",
           "p=> p",
           //that's the "select" part, so put what you need in it
           "(p, grouping) => {key: p, values: grouping.source}")                
  .ToArray();

Then you can select what you need.

Sadly, it seems (or I didn't get how to do it) a group by multiple fields doesn't work as it should (it returns multiple records).

While .GroupBy("p => p.dte}", works as expected.

Raphaël Althaus
  • 59,727
  • 6
  • 96
  • 122
0

First, it's important to understand what a linq query in the query syntax is when converted to using the method call syntax.

(from y in class1
 join x in class2 on y.Id equals x.Name
 group y by new { y.Date, x.Number } into xy
 select new class3()
 {
 }).ToList();

The C# equivalent:

class1.Join(class2, y => y.Id, x => x.Name, (y, x) => new { y, x })
    .GroupBy(z => new { z.y.Date, z.x.Number })
    .Select(xy => new class3())
    .ToList();

Then it should be simple converting to the Linq.js equivalent.

var query =
    class1.Join(class2, "$.Id", "$.Name", "{ y: $, x: $$ }")
        .GroupBy(
            "{ Date: $.y.Date, Number: $.x.Number }",
            null,
            null,
            "$.Date + ' ' + $.Number"
        )
        .Select("new class3()")
        .ToArray();

Note that since we're using an object as a key, we must provide a compare selector.

Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272