2009-12-30 163 views
0

如何在linq VB.NET中编写此查询?Linq to SQL by group by

select top 15 count(1), A.Latitude, A.Longitude 
from Bairro A 
inner join Empresa B on B.BairroID = A.BairroID 
where A.CidadeID = 4810 
group by A.Latitude, A.Longitude 
order by COUNT(1) desc 

我达到了这个代码:

Dim TopBairros = (From A In Bairros _ 
        Join B In New BO.Empresa().Select On B.BairroID Equals A.BairroID Group A By A.Latitude, A.Longitude Into g = Group _ 
        Select g Distinct _ 
        Order By g.Count Descending).Take(15) 

每行具有含有屡与计数数的同一对象阵列集合。例如:

行0:874个相同的对象 第1排阵:710个相同的对象

等的阵列......我该怎么办每行返回只有一个对象?

回答

3

试试这个:

var query = from a in context.Bairro 
      where a.CidadeID == 4810 
      join b in context.Empresa on a.BairroID equals b.BairroID 
      group a by new { a.Latitude, a.Longitude } into grouped 
      orderby grouped.Count() descending 
      select new { grouped.Key.Latitude, 
         grouped.Key.Longitude, 
         Count = grouped.Count() }; 
var top15 = query.Take(15); 
+0

我使用VB.NET ......我达到了这个代码: 昏暗TopBairros =(从在Bairros _ JOIN B在新BO.Empresa()。选择On B.BairroID Equals A.BairroID Group A按A.Latitude,A.Longitude进入g = Group _ 选择g Distinct _ Order by g.Count Descending).Take(15) 每行都有一个数组集合重复包含与计数相同的对象。例如: 第0行:874个相同对象的数组 第1行:710个相同对象的数组 等等......如何才能每行只返回一个对象? – Fernando 2009-12-30 23:51:30

+0

@Fernando:通过使用最后得到的投影 - 选择组的关键和计数。 – 2009-12-31 00:49:06