2010-03-02 4 views
1

일대 다 관계에서 적절한 LINQ 쿼리 또는 람다 식을 작성하는 방법을 생각하면서 "많은"관계에있는 엔터티의 적절한 필터링을 수행했습니다. Entity Framework의 측면. 필터를 사용하여 일대 다 LINQ 쿼리 또는 람다 식을 작성하는 방법

그래서 두 기관은 다음과 같습니다

레시피
ID
이름 유형 [작은 | 큰]

성분이
ID
일반
이름
유형을 [recipeId 이국적인]

작고 이국적인 성분을 가진 조리법을 선택하는 LINQ 쿼리를 작성하는 방법은 무엇입니까?

var smallExoticRecipes = contex.Recipes.Include("Ingredients").Where(recipe => recipe.Type == "small" && ????); 

내가 대신 "????"의 작성해야 할 :

이 같이 갈 수 있을까? 아니면 람다 식 대신 LINQ 쿼리를 시도해야합니까?

UPDATE : 나는 그들도있을 수 있지만, 일반 사람없이 단지 조리법과 이국적인 재료를 선택하고 싶은 절을 "선택"에
?

그래서 :

나는 이렇게 가야합니까? 물론

.Select(recipe => new { recipeName = recipe.Name, recipeIgredients = recipe.Ingredients.Where(ing => ing.Type == "exotic" }); 

답변

2
var smallExoticRecipes = contex.Recipes.Include("Ingredients").Where(recipe => recipe.Type == "small" && recipe.Ingredients.Any(i => i.type == "exotic")); 

, 당신은 명확성을 위해이를 나눌 수

Func<Recipe, bool> hasExoticIngredients = r => r.Ingredients.Any(i => i.type == "exotic"); 
var smallExoticRecipes = context.Recipes.Include("Ingredients").Where(recipe => recipe.Type == "small" && hasExoticIngredients(recipe)); 

또 다른 옵션은 다음과 같습니다

Func<Recipe, bool> hasExoticIngredients = r => r.Ingredients.Any(i => i.type == "exotic"); 
Func<Recipe, bool> isSmallAndExotic = r => recipe => recipe.Type == "small" && hasExoticIngredients(recipe) 
var smallExoticRecipes = context.Recipes.Include("Ingredients").Where(isSmallAndExotic); 
0
var smallExoticRecipes = contex.Recipes.Include("Ingredients").Where(recipe => recipe.Type == "small" && ingredients.Any(ingredient => ingredient.recipeid == recipe.id && ingredient == "exotic")); 
관련 문제