0

SQL:

SELECT i.name, l.city, COUNT( l.city ) AS num
FROM locality l
JOIN event e ON e.ID_locality = l.ID
JOIN program p ON p.ID_event = e.ID
JOIN interpreter i ON i.ID = p.ID_interpreter
WHERE i.name = 'XXXX'
GROUP BY l.city
HAVING COUNT( l.city) = (
SELECT MAX( num ) 
FROM ( SELECT COUNT( city ) AS num
FROM (SELECT l.city FROM locality l
JOIN event e ON e.ID_locality = l.ID
JOIN program p ON p.ID_event = e.id
JOIN interpreter i ON i.ID = p.ID_interpreter
WHERE i.name = 'XXXX'
GROUP BY l.city) tmp ) tmp2)

林克:

var q4 = from l in db.locality
         join e in db.event on l.ID equals e.ID_locality
         join p in db.program on e.ID equals p.ID_event
         join i in db.interpreter on p.ID_interpreter equals i.ID
         where i.name == "XXXX"
         group l by l.city into grp

你好,我对 LINQ 中的这个选择有严重的问题。我用 SQL 编写了这个选择,但我不知道如何通过计数来使用多子选择和组......我想选择解释器“XXXX”最常行动的城市以及解释器在那里行动的次数。我还在这里检查了输入链接描述,但是当我尝试使用它时它没有帮助我。如果有人可以帮助我,我会很高兴。为了更好的定位:数据库表

4

1 回答 1

0

我不确定,但我希望你能做到,类似的事情。

var query = (from l in db.locality
            join e in db.event on l.ID equals e.ID_locality
            join p in db.program on e.ID equals p.ID_event
            join i in db.interpreter on i.ID equals p.ID_interpreter
            group l by new { i.name, l.city } into g
            where i.name == "XXXX" &&
            g.Count(x => x.city) == (from l in db.locality
                                            join e in db.event on l.ID equals e.ID_locality
                                            join p in db.program on e.ID equals p.ID_event
                                            join i in db.interpreter on i.ID equals p.ID_interpreter
                                            where i.name == "XXXX"                  
                                            group l by l.city into g
                                            select new 
                                            {
                                                cityCount = g.Count(x => x.city)
                                            }).Max()

            select new 
            {
                g.key.name,
                g.key.city,
                maxNum = g.count(x => x.city)
            }).ToList();
于 2013-05-05T11:58:55.580 回答