LINQ: Using INNER JOIN, Group and SUM

前端 未结 3 1651
遇见更好的自我
遇见更好的自我 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: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) 
                };
    

提交回复
热议问题