无法使用Group By将SQL转换为Linq

时间:2017-06-30 14:44:32

标签: sql-to-linq-conversion

我正在尝试将此sql转换为linq,但到目前为止还无法实现 下面的代码成功地给出了我需要的结果

SELECT [dbo].[Ingredient].[IngredientId]
,[EnglishName]
,[HindiName]
,COUNT([dbo].[RecipeIngredient].RecipeId) As IngredientUsed

FROM [DB_A19C63_kmkb].[dbo].[Ingredient]

JOIN [dbo].[RecipeIngredient] 
ON [dbo].[RecipeIngredient].IngredientId = [dbo].[Ingredient].[IngredientId] 
WHERE [dbo].[RecipeIngredient].IngredientId = 39

GROUP BY [dbo].[Ingredient].[IngredientId]
,[EnglishName]
,[HindiName]


Result
IngredientId    EnglishName   HindiName IngredientUsed
39             Asafoetida     Hing      628

我的Linq代码

(from x in _unitOfWork.IngredientRepository.GetManyQueryable()

                          select new IngredientEntity
                          {
                              IngredientId = x.IngredientId,
                              EnglishName = x.EnglishName,
                              HindiName = x.HindiName,
                              Pros = x.Pros,
                              Cons = x.Cons,
                              Calories = x.Calories,
                              IngredientUsed = (from z in _unitOfWork.RecipeIngredientRepository.GetAll()
                                                where z.IngredientId == x.IngredientId
                                                select z.RecipeId).Count(),
                              DepartmentId = x.DepartmentId

                          }).OrderBy(x => x.EnglishName);

先谢谢

0 个答案:

没有答案