简体   繁体   English

查询实体选择,内部联接和计数

[英]Query entity select, inner join and count

i want to translate this SQL query : 我想翻译这个SQL查询:

SELECT FirstName,LastName, WaiterId, Count(*) AS compte 
FROM Waiter
INNER JOIN Client on Waiter.Id = Client.WaiterId
GROUP BY WaiterId,lastname, firstname 
ORDER BY compte DESC;

in entity framework. 在实体框架中。

I tried something like this : 我尝试过这样的事情:

           var query = (from w in db.Waiter
                     join c in db.Client on w.Id equals c.WaiterId
                     group c by c.WaiterId into g
                     //orderby 
                     select new
                     {
                         WaiterId = g.Key,
                         count = g.Count()
                     });

but my select don't work. 但我的选择不起作用。 I can't select FirstName and LastName and i don't even know if my count is good. 我无法选择名字和姓氏,我什至不知道我的人数是否不错。

You need to include all the properties in the group by. 您需要在分组依据中包括所有属性。

var query = (from w in db.Waiter
             join c in db.Client on w.Id equals c.WaiterId
             group c by new { c.FirstName, c.LastName, c.WaiterId} into g
             orderby g.Count() descending
             select new
             {
                  FirstName = g.Key.FirstName,
                  LastName  = g.Key.LastName,
                  WaiterId  = g.Key.WaiterId,
                  count     = g.Count()
              });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM