public static List <FoodAndIngredient> GetFoodAndIngredientsFromFoodID(int food_id) { string stmt = "select id, food_id,ingredient_id from foodingredient where food_id = @food_id"; using (var conn = new NpgsqlConnection(connectionString)) { FoodAndIngredient foodAndIngredient = null; List <FoodAndIngredient> foodandingredients = new List <FoodAndIngredient>(); conn.Open(); using (var command = new NpgsqlCommand(stmt, conn)) { command.Parameters.AddWithValue("food_id", food_id); using (var reader = command.ExecuteReader()) { while (reader.Read()) { foodAndIngredient = new FoodAndIngredient { ID = (int)reader["id"], Food_id = (int)reader["food_id"], Ingredient_id = (int)reader["ingredient_id"], }; foodandingredients.Add(foodAndIngredient); } } return(foodandingredients); } } }
private void Register_FoodAndIngredient(List <Ingredient> ingredients, Food food) { List <FoodAndIngredient> foodAndIngredients = new List <FoodAndIngredient>(); foreach (var ingredient in ingredients) { FoodAndIngredient foodAndIngredient = new FoodAndIngredient(); foodAndIngredient.MakeFoodAndIngredient(food.ID, ingredient.ID); foodAndIngredients.Add(foodAndIngredient); } AddFoodandIngredients(foodAndIngredients); }