LINQ: Using INNER JOIN, Group and SUM

前端 未结 3 1642
遇见更好的自我
遇见更好的自我 2020-11-30 04:46

I am trying to perform the following SQL using LINQ and the closest I got was doing cross joins and sum calculations. I know there has to be a better way to write it so I am

相关标签:
3条回答
  • 2020-11-30 05:28

    Below code is working for me :

                              var credit = (from bm in BulkMessage
                              join sms in SMS on bm.BulkMessageId equals sms.BulkMessageId
                              where bm.ProfileId == pid && bm.IsActive == true
                               group sms by sms.SMSCredit into g
    
                              select new { SMSCredits = g.Sum(s => s.SMSCredit) }).FirstOrDefault();
    
    0 讨论(0)
  • 2020-11-30 05:31

    For me (using 4.0), the following works.

    var total = from T1 in context.T1
                join T2 in context.T2 on T1.T2ID equals T2.T2ID
                join T3 in context.T3 on T2.T3ID equals T3.T3ID
                group T3 by new { T1.Column1, T1.Column2 } into g
                select new { 
                    Column1 = g.Key.Column1, 
                    Column2 = g.Key.Column2, 
                    Amount = g.Sum(t3 => t3.Column1) 
                };
    
    0 讨论(0)
  • 2020-11-30 05:37

    Try this:

    var total = from T1 in context.T1
                join T2 in context.T2 on T1.T2ID equals T2.T2ID
                join T3 in context.T3 on T2.T3ID equals T3.T3ID
                group T3 by new { T1.Column1, T1.Column2 } into g
                select new { 
                    Column1 = T1.Column1, 
                    Column2 = T2.Column2, 
                    Amount = g.Sum(t3 => t3.Column1) 
                };
    
    0 讨论(0)
提交回复
热议问题