使用Group by进行LInq到Sql查询

时间:2012-07-24 07:55:15

标签: c# sql linq linq-to-sql

我正在尝试将linq编写为等同于以下sql查询的SQL查询:

select * from [TableA]
INNER JOIN (select C1, COUNT(*) C2 FROM [Table2] GROUP BY C1) av ON [TableA].C1 = av.C1
WHERE av.C2 > 10

任何人都可以帮我解决如何编写等效的链接查询。

1 个答案:

答案 0 :(得分:2)

var query = from b in context.TableB
             group new { c1 } by new
             {
                b.c1
             } into GroupByC1
             select new 
             {
                c1 = GroupByC1.Key.C1,
                count1 = GroupByC1.count()
             } 

var result = from a in tableA
             join b in query on a.c1 equals b.c1
             where b.count1 > 10