2016-06-20 3 views
1

일부 Linq-querys를 LinqJs- 쿼리로 변환하여 LinqJS를 배우려고했습니다.linq-query를 linqjs로 변환

이것은 Linq- 쿼리입니다.

(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(); 

이 현재 시도는 (여러 번 다시 작성되었습니다). 나는 구문을 이해하지 못한다고 생각합니다.

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(); 

답변

0

글쎄, 당신은이

먼저 같은 조인 부분이 뭔가를 할 수 있습니다.

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(); 

는 그런 부분에 의해 그룹

 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(); 

그런 다음 당신은 당신이 필요로 선택할 수 있습니다 (당신은 물론 하나에 모두 할 수있다).

슬프게도, 여러 필드에 의해 그룹이 작동하지 않는 것처럼 보입니다 (여러 레코드가 반환 됨).

.GroupBy("p => p.dte}",은 예상대로 작동합니다.

0

먼저 쿼리 구문에서 linq 쿼리가 메서드 호출 구문을 사용하여 변환 될 때 무엇인지 이해하는 것이 중요합니다.

(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(); 

는 C# 당량

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(); 

그렇다면 그것은 Linq.js 당량 변환 간단해야한다. 우리가 키로서 객체를 사용하고 있기 때문에, 우리는 비교 선택을 제공해야

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

참고.