具有HAVING SUM的Linq中的SQL查询 [英] SQL Query in Linq with HAVING SUM

查看:51
本文介绍了具有HAVING SUM的Linq中的SQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含三个表的小型数据库示例:城市,国家/地区和地理语言(国家/地区使用的语言).现在,我想获得世界上所有人口至少一百万的城市中使用最多的语言.

I have a small database example with three tables: Cities, Country and Geo_Languages (Languages spoken in the countries). Now I want to get the most spoken languages of all cities in the world with a population of at least one million people.

这是SQL查询:

SELECT SUM(c.population) AS totalPop, g.name_en
FROM cities c,  country cy,  geo_languages g
WHERE   c.country_code = cy.id AND 
    cy.id = g.code2l
GROUP BY g.name_en
HAVING SUM(c.population) > 1000000
ORDER BY totalPop DESC;

到目前为止,这里是Linq查询:

Here the Linq-Query so far:

Var query =
    from c in db.City
    join country in db.Country on c.country_code equals country.id
    join languages in db.geo_languages on country.id equals
        languages.code2l    
    group languages by languages.name_en    
    select new{
        totalPop = c.Sum (c => c.population)    
    };

我只是不知道如何将HAVING SUM和ORDER BY转换为Linq.感谢您的帮助.

I just don't know how to convert the HAVING SUM and the ORDER BY into Linq. I'm thankful for any help.

推荐答案

尝试一个:

var query =
    from c in db.City
    join country in db.Country on c.country_code equals country.id
    join languages in db.geo_languages on country.id equals
        languages.code2l    
    group c by languages.name_en into g
    where g.Sum(x => x.population) > 1000000
    select new {
        totalPop = g.Sum(x => x.population)    
    };

这篇关于具有HAVING SUM的Linq中的SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆