/// <summary> /// Method to search the database for recipes matching the given categories and/or contexts. /// </summary> protected void SearchCategoriesAndContexts() { List <string> categories = GetCategories(); List <string> contexts = GetContexts(); using (Models.PlanMyDinnerDbContext database = new Models.PlanMyDinnerDbContext()) { var recipes = database.Recipes.Where(p => (categories.Count > 0 && p.Categories.Any(c => categories.Contains(c.Name.ToString()))) && (contexts.Count > 0 && p.Contexts.Any(c => contexts.Contains(c.Name.ToString())))).Select(p => p); SearchCleanUp(recipes); } }
protected void ButtonClear_Click(object sender, EventArgs e) { ClearAllGridviews(); SetAllCheckBoxes(); using (Models.PlanMyDinnerDbContext database = new Models.PlanMyDinnerDbContext()) { //Using TemplateFields in the GridView Control // https://msdn.microsoft.com/en-us/library/bb288032.aspx RecipeGridView.DataSource = database.Recipes.ToList(); RecipeGridView.DataBind(); } ltRecipe.Text = "Showing all available recipes"; }
/************************************************************************* * * Database/model search methods * * ***********************************************************************/ /// <summary> /// Method to search the database for recipes and/or ingrerdients matching the /// given search string. The search algortihm considers /// - if checkbox 'Recipes' is checked - include only matches which include the search string in recipe name /// - if checkbox 'Ingredients' is check - include only matches which include the search string in ingredient names /// - if both checkboxs 'Recipes' and 'Ingredients' are checked - inlcude only matches which contain the search string in' /// recipe name AND ingredient names. /// - if neither checkbox 'Recipes' or 'Ingredients' are checked - the search result will be empty /// /// The search algorithm also filters the search so that /// - only selected categories are included in the search result /// - only selected contextx are included in the search result /// https://stackoverflow.com/questions/16993962/searching-multiple-fields-with-linq-contains-or-other /// https://stackoverflow.com/questions/41333535/using-if-else-in-lambda-expression-in-where-clause /// https://stackoverflow.com/questions/3177113/lambda-expression-for-exists-within-list /// </summary> /// <param name="searchString"></param> protected void SearchIngredientAndRecipeNames(string searchString) { List <string> categories = GetCategories(); List <string> contexts = GetContexts(); using (Models.PlanMyDinnerDbContext database = new Models.PlanMyDinnerDbContext()) { var recipes = database.Recipes.Where(p => ((categories.Count > 0 && p.Categories.Any(c => categories.Contains(c.Name.ToString()))) && //filter selected categories (contexts.Count > 0 && p.Contexts.Any(c => contexts.Contains(c.Name.ToString())))) && //filter selected categories ((CheckboxRecipe.Checked && p.Name.Contains(searchString)) || //filter recipe name (CheckboxIngredient.Checked && p.Ingredients.Any(c => c.Name.Contains(searchString))))).Select(p => p); //filter ingredient names SearchCleanUp(recipes); } }
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack)//Prevent to GUI reset at reload { ClearAllGridviews(); using (Models.PlanMyDinnerDbContext database = new Models.PlanMyDinnerDbContext()) { //Using TemplateFields in the GridView Control // https://msdn.microsoft.com/en-us/library/bb288032.aspx UpdateRecipeGrid(database.Recipes); } ltRecipe.Text = "Showing all available recipes"; } }
protected void UpdateReceipeDetailsGridViews(int recipeId) { using (Models.PlanMyDinnerDbContext database = new Models.PlanMyDinnerDbContext()) { IngredientGridView.DataSource = database.Recipes.Find(recipeId).Ingredients.ToList(); IngredientGridView.DataBind(); CategoryGridView.DataSource = database.Recipes.Find(recipeId).Categories.ToList(); CategoryGridView.DataBind(); ContextGridView.DataSource = database.Recipes.Find(recipeId).Contexts.ToList(); ContextGridView.DataBind(); ltInstructions.Text = addHtmlBreakTags(database.Recipes.Find(recipeId).Instructions); string CurrentDir = new DirectoryInfo(Environment.CurrentDirectory).Parent.Parent.FullName; string ImagePath = System.IO.Path.Combine(CurrentDir, "Media/" + database.Recipes.Find(recipeId).ThumbFileName); List <ListItem> Imgs = new List <ListItem>(); string ImgName = Path.GetFileName(ImagePath); Imgs.Add(new ListItem(ImgName, "~/Media/" + ImgName)); Gv_imgs.DataSource = Imgs; Gv_imgs.DataBind(); } }