I am sending an ASP Awesome Combobox entries for a dropdown using JSON.
Code below:
public ActionResult GetEngineerData()
{
var items = db.Results.Where(x=>x.Engineer != null).Select(o=>new KeyContent(o.Engineer,o.Engineer)).Distinct()
return Json(items);
}
This does not provide a distinct list, instead there are many duplicates. I have also tried doing this (positioning distinct before the Select KeyContent code):
public ActionResult GetEngineerData()
{
var items = db.Results.Where(x=>x.Engineer != null).Distinct().Select(o=>new KeyContent(o.Engineer,o.Engineer))
return Json(items);
}
Thanks for any help.
CodePudding user response:
when creating a new KeyContent
, you get distinct instances - different objects, even if their values are the same.
that's why your first code doesn't work.
in your second one, you check if the Results
are distinct, and for each distinct one, you select the Engineer
.
If you want distinct engineers, try this:
public ActionResult GetEngineerData()
{
var items = db.Results
.Where(x=>x.Engineer != null) //eliminate empty values
.Select(x=>x.Engineer) //select only the engineer-item
.Distinct() //remove _duplicate engineers_ from the list
.Select(x=>new KeyContent(x,x));//for each unique engineer, create a KeyContent instance
return Json(items);
}