【发布时间】:2013-05-22 21:27:22
【问题描述】:
我有带有食谱的表格和带有食谱类别的表格。另一个表中有不同的类别类型。我有 optional 类别参数,我的表格和 linq 选择看起来像:
表格:
RECIPE
recipeId title
RECIPE CATEGORIES
recipeCategoryId recipeId categoryId categoryTypeId
var result = from r in context.Recipes
join c in context.RecipeCategories on r.recipeId equals c.recipeId
where (nutritionStyleId == 0 || c.categoryId == nutritionStyleId )
&& (courseId == 0 || c.categoryId == courseId)
select new
{
r.recipeId,
r.title
};
您只能按一种类型选择类别(按 courseId 或按 NutritionStyleId 或无(如果 NutritionStyleId 和 courseId == 0)。问题是当 NutritionStyleId courseId 为 0,然后选择返回我 all recipes 但乘以 RECIPE CATEGORIES 表中 categoryTypeId's 的数量. 所以如果一个菜谱有更多的 categoryType 指定,我的选择是错误的。
那么我怎样才能使 conditional 加入什么的,所以当我不想按 recipeCategory 搜索时,不会有任何重复(重复的标题或 recipeId)
场景是我可以通过 NutritionStyleId 搜索 OR courseId OR nothing(return all recipes)
【问题讨论】:
标签: c# .net linq entity-framework