我有一個關於LINQ和foreach的問題。可以說我有一個簡單的DB有三個表格(ingredient
,recipe
和多對多關係ingredientinrecipe
)。他們都有簡單的id和名字結構。LINQ語句獲取每個配方的ingridients列表
我正在運行控制檯應用程序,其中用戶可以輸入兩個ingredients
例如
egg, flour
我怎麼會結構中的LINQ條款要經過所有的食譜,這些地方都將是內部的。
我使用UOW一起構建的一切,所以我可以做uow.Ingridients.All
...等等等等
我想我需要從ingridientinrecipe
表接近它。
這將是以下的東西:
var recipes = uow.ingridientinrecipe.All.Where(a => a.Ingridient.IngridientName.Contains(...
但是,當且僅當用戶進入一個ingridient這個工程。我將如何擴展它以使用兩個無限輸入。
我添加了我迄今爲止所做的。
var line = Console.ReadLine();
while (line.ToLower().Trim() != "exit")
{
var ingridients= line.Split(',');
Console.WriteLine("\nAre you looking for following receipes:");
foreach (var ingridient in ingridients)
{
var recipes= _uow.IngridientInRecipe.All.Where(a => a.Ingridient.IngridientName.Contains(ingridient)).ToList();
foreach (var recipe in recipes)
{
Console.WriteLine(recipe.Recipes.RecipeName);
}
Console.WriteLine();
line = Console.ReadLine();
}
表結構,是ASKED
public class Recipe
{
public int RecipeId{ get; set; }
public string RecipeName{ get; set; }
public virtual List<IngridientInRecipe> IngridientInRecipe{ get; set; }
}
public class Symptom
{
public int IngridientId{ get; set; }
public string IngridientName{ get; set; }
public virtual List<IngridientInRecipe> IngridientInRecipe{ get; set; }
}
public class IngridientInRecipe
{
public int IngridientInRecipeId{ get; set; }
public int RecipeId{ get; set; }
public virtual Recipe Recipe{ get; set; }
public int IngridientId{ get; set; }
public virtual Ingridient Ingridient{ get; set; }
}
我t的拼寫「成分」 –
我一直在試圖寫解決方案,但我需要你分享表結構,請。 – Veverke
@Veverke已添加! –