0

I am trying to get the ID and name of multiple objects from my database when a user submits a form. I don't want any repeats though when it displays the results.

I have my Form:

@using (Html.BeginForm("Advancedresults", "Search", FormMethod.Post))
        {
            {
                foreach (var r in ViewBag.res)
                {
                    string hes = r.iname;
                    <div class="col-md-4">
                        @Html.CheckBox("drink", false, new { value = hes })
                        @r.iname
                    </div>
                }
                <input type="submit" />
            }
        }

This is sent to my Search Controller using the HttpPost Method:

[HttpPost]
    public ActionResult Advancedresults()
    {
        string drinks = Request.Form["drink"];
        List<string> dList = drinks.Split(',').ToList();
        dList = dList.Where(x => x != "false").ToList();
        List<string> r = new List<string>();

        foreach (string st in dList)
        {

            r.AddRange(from a in db.Recipes.AsEnumerable()
                       join b in db.RecipeIngredients on a.ID equals b.recipe_id
                       join i in db.Ingredients on b.ingredient_id equals i.id
                       join u in db.Measures on b.measure_id equals u.id
                       where i.name.Equals(st)
                       select a.name
                      );
        }

        List<string> ra = new List<string>();

        foreach (string ras in r)
        {
            if (!ra.Contains(ras))
            {
                ra.Add(ras);
            };
        }


        ViewBag.Ingredients = ra.ToList();
        return View();
    }

Is there a better way of adding the ID and name to the ViewBag? I know what I am doing now is not best practice.

2 Answers 2

2

You could probably simplify it by using Contains and Distinct

var results = (from a in db.Recipes.AsEnumerable()
           join b in db.RecipeIngredients on a.ID equals b.recipe_id
           join i in db.Ingredients on b.ingredient_id equals i.id
           join u in db.Measures on b.measure_id equals u.id
           where dList.Contains(i.name)
           select a.name ).Distinct();

ViewBag.Ingredients = results.ToList();
Sign up to request clarification or add additional context in comments.

Comments

2

You can use the Distinct() method of LINQ to select unique values from a list. Example:

List<string> ra = new List<string>(r.Distinct());

Regarding to your question if you can add both Name and ID to the ViewBag, I suggest for you to use Dictionary:

Dictionary<int, string> Results = new Dictionary<int, string>();

Given that, you can update your LINQ query to something like this:

var results = (from a in db.Recipes.AsEnumerable()
       join b in db.RecipeIngredients on a.ID equals b.recipe_id
       join i in db.Ingredients on b.ingredient_id equals i.id
       join u in db.Measures on b.measure_id equals u.id
       where dList.Contains(i.name).GroupBy(a => a.ID, a=>a.Name).ToDictionary(s=>s.Key, v=>v.First());

ViewBag.Ingredients = results.ToList();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.