2011-08-18 5 views
0

이 표현식을 SQL에서 LINQ로 변환하려고 시도하지만 나에게 너무 어렵습니다. 어쩌면이 문제를 해결할 수 있습니다!하위 쿼리를 사용하여 복잡한 SQL에서 LINQ로 변환

SELECT  TOP (2) RecipeID, UserID, Name, Servings, PreparationTime, TotalTime, DifficultyLevelID, CuisineID, DishID, MainIngredientID, PriceLevelID, FlavourID, Instructions, 
         Notes, Thumbnail, VideoLink 
FROM   dbo.Recipes 
WHERE  (RecipeID NOT IN 
          (SELECT DISTINCT Recipes_1.RecipeID 
          FROM   dbo.Allergies INNER JOIN 
                dbo.UsersAllergies ON dbo.Allergies.AllergyID = dbo.UsersAllergies.AllergyID INNER JOIN 
                dbo.IngredientsAllergies ON dbo.Allergies.AllergyID = dbo.IngredientsAllergies.AllergyID INNER JOIN 
                dbo.Ingredients ON dbo.IngredientsAllergies.IngredientID = dbo.Ingredients.IngredientID INNER JOIN 
                dbo.RecipesIngredients ON dbo.Ingredients.IngredientID = dbo.RecipesIngredients.IngredientID INNER JOIN 
                dbo.Recipes AS Recipes_1 ON dbo.RecipesIngredients.RecipeID = Recipes_1.RecipeID INNER JOIN 
                dbo.Users ON dbo.UsersAllergies.UserID = dbo.Users.UserID INNER JOIN 
                dbo.AllergyFactors ON dbo.IngredientsAllergies.AllergyFactorID = dbo.AllergyFactors.AllergyFactorID 
          WHERE  (dbo.Users.UserID = 3) AND (dbo.AllergyFactors.AllergyFactorID < 3))) 

답변

1

당신이 이미 시도 것을 우리에게 보여 주었다, 그러나이 같은 Linq를 발현 당신에게 동일한 결과를 제공해야하는 경우 당신을 돕기 위해 쉬울 것,

var query = (from rec in context.Recipes 
      where !(from al in context.Allergies 
        from ua in context.UsersAllergies.Where(x => al.AllergyID == x.AllergyID) 
        from ia in context.IngredientsAllergies.Where(x => al.AllergyID == x.AllergyID) 
        from in in context.Ingredients.Where(x => ia.IngredientID == x.IngredientID) 
        from ri in context.RecipesIngredients.Where(x => in.IngredientID == x.IngredientID) 
        from re in context.Recipes.Where(x => ri.RecipeID == x.RecipeID) 
        from us in context.Users.Where(x => ua.UserID == x.UserID) 
        from af in context.AllergyFactors.Where(x => ia.AllergyFactorID == x.AllergyFactorID) 
        where us.UserID == 3 && af.AllergyFactorID < 3 
        select re.RecipeID) 
        .Distinct() 
        .Contains(rec.RecipeID) 
      select new 
      { 
       rec.RecipeID, 
       rec.UserID, 
       rec.Name, 
       rec.Servings, 
       rec.PreparationTime, 
       rec.TotalTime, 
       rec.DifficultyLevelID, 
       rec.CuisineID, 
       rec.DishID, 
       rec.MainIngredientID, 
       rec.PriceLevelID, 
       rec.FlavourID, 
       rec.Instructions, 
       rec.Notes, 
       rec.Thumbnail, 
       rec.VideoLink 
      }).Take(2); 
+0

이 쿼리가 깨진 설정을했다 어쩌면 뭔가 잘못 입력 했습니까? – laszloj

+0

@ st4rlight - 모델에 대한 세부 정보를 제공하지 않았으므로 이름을 추측 할 때 최선을 다했습니다. 좀 더 정확한 대답을 원할 경우 코드 일부를 보여줄 필요가 있습니다 – Aducci

+0

한편 나는 저장 프로 시저로갔습니다. 당신의 도움을 주셔서 감사합니다! – laszloj

관련 문제