主要目标是根据受欢迎程度(根据有多少人最喜欢的特定成分表示)以排序顺序返回成分列表。SQL 等价物如下:
select distinct COUNT(PersonalInfoes.FavoriteIngredient_ID) as NoUsing,
Ingredients.Name
from PersonalInfoes right join Ingredients
on PersonalInfoes.FavoriteIngredient_ID = Ingredients.ID
group by Ingredients.Name
order by NoUsing desc
产生与上述相同结果的方法语法的等效 SQL 查询是什么?我得到的最接近的是:
from i in db.Ingredients
join p in db.PersonalInfos on i.ID equals p.FavoriteIngredient.ID into ing
from p in ing.DefaultIfEmpty()
group i by i.Name into g
from p in g
orderby g.Count() descending
select new { Name = g.Key, Count = g.Count() }.Name;