从dropdownlist数据库中删除重复项

时间:2016-07-24 09:58:51

标签: .net

我使用Entity Framework从数据库生成了下拉列表。我可以在视图中看到我的下拉列表,但我得到重复的值。如何删除重复的值?

controller:

DBEntities db = new DBEntities ();
var a = db.AspNetUsers.Select(h => new SelectListItem
                {
                    Value = h.Nationality,
                    Text = h.Nationality
                }).ToList();
            ViewData["nationselection"] = new SelectList(a, "Value", "Text");


View:

 @Html.DropDownList("selectedItem", (SelectList)ViewData["nationselection"], "-Selce-", new
    {

        onchange = @"
            var form = document.forms[0];
            form.action='GetEmployeeDetails';
            form.submit();"
    })

1 个答案:

答案 0 :(得分:0)

I understand you have duplicate values in your database. In EF u can select a distinct values from a db context.

DBEntities db = new DBEntities();
var a = db.AspNetUsers.Select(h => new SelectListItem
                {
                    Value = h.Nationality,
                    Text = h.Nationality
                }).**Distinct().ToList();**