Deligny Gaëtan
Deligny Gaëtan

Reputation: 33

Query entity select, inner join and count

i want to translate this SQL query :

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.

Upvotes: 0

Views: 2137

Answers (1)

Magnus
Magnus

Reputation: 46929

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

Upvotes: 2

Related Questions