开发者

Grouping troubles

Hi i am having difficulties to group these data as it has no aggregate function. I have the following data in 2 tables and i would like to Join both by PaymentId and display only 1 row of record.

Grouping troubles

Question:

How do i display the final result in only 1 ROW groupby CoursePaidForMonthYear.

I would like to get all data from Payment.*, TutorCourseCommissions.* and CoursePaidForMonthYear column in same row displaying (September, October, November)

Example:

referenceId| TutorId| CoursePaidForMonthYear                 |  
1          | 1019   | September, October, November OR 9,10,11|

My work:

 var result = from u in db.Users
                    join p in db.Payments on u.Id equals p.UserId
                    j开发者_开发技巧oin tt in db.TutorCourseCommissions on p.Id equals tt.PaymentId into gtt
                        from tt in gtt.DefaultIfEmpty()                       
                    where u.Id == user.Id
                    GroupBy tt.CoursePaidForMonthYear  ??
                    select new { u, p, tt };

            foreach (var r in result)
            {
                Payment payment = new Payment();                  
                payment.MonthToPay = (r.tt == null) ? null : common.GetMonthName(r.tt.CoursePaidForMonthYear.Month, true);
                payment.Amount = r.p.Amount;                    

                output.Add(payment);
            }
            return output;


What you need to do is group the months by the user and payment, and then perform an aggregation on the grouped months. In this case I used String.Join() to combine the distinct months coming from the commission.

This will give you results along the lines of { User = "John Doe", Payment = ..., Months = "January, February" }

var result = from u in db.Users
              where u.UserID == user.Id
              join p in db.Payments on u.Id equals p.UserId
              join comm in db.TutorCourseCommissions 
              on p.Id equals comm.PaymentId
              group common.GetMonthName(comm.CoursePaidForMonthYear,true)
              by new { User = u, Payment = p } into g
              select new 
              {
                 User = g.Key.User,
                 Payment = g.Key.Payment,
                 //...select out other properties here... 
                 Months = String.Join(", ", g.Distinct())
              };


Made a class with properties regarding all columns you required and then do the selection using its instance ...

Like

public TutorPayments
{

 public int UserID{
 get;
 set;
}

 public int PayType{
 get;
 set;
  }

  public int TutorID{
  get;
 set;
  }

 //and so on all columns property that you required
}

now you can get all these in your query by creating its instance in this way

   var result = from u in db.Users
                join p in db.Payments on u.Id equals p.UserId
                join tt in db.TutorCourseCommissions on p.Id equals tt.PaymentId into gtt
                    from tt in gtt.DefaultIfEmpty()                       
                where u.Id == user.Id
                GroupBy tt.CoursePaidForMonthYear  ??
                select new TutorPayments {UserID =  u.id,PayType = p.id,TutorID = tt.TutorId, ............ };
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜