【问题标题】:Linq to SQL with group byLinq to SQL with group by
【发布时间】:2010-12-31 06:43:04
【问题描述】:

如何在 linq VB.NET 中编写此查询?

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 个相同对象的数组

等等...如何每行只返回一个对象?

【问题讨论】:

    标签: sql vb.net linq linq-to-sql linq-group


    【解决方案1】:

    试试这个:

    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);
    

    【讨论】:

    • 我正在使用 VB.NET... 我到达了这个代码: 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 个相同对象的数组,依此类推...如何每行仅返回一个对象?
    • @Fernando:通过使用我最后得到的投影 - 选择组的键和计数。
    猜你喜欢
    • 1970-01-01
    • 2017-05-29
    • 1970-01-01
    • 2010-12-13
    • 1970-01-01
    • 2015-04-26
    • 2010-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多