【发布时间】:2016-11-07 17:36:06
【问题描述】:
我的数据库中有一个字段重复。我想在一个下拉列表中使用它,它必须返回不同的数据。
这是我为此创建的方法:
public IEnumerable<SelectListItem> GetBranches(string username)
{
using (var objData = new BranchEntities())
{
IEnumerable<SelectListItem> objdataresult = objData.ABC_USER.Select(c => new SelectListItem
{
Value = c.BRANCH_CODE.ToString(),
Text = c.BRANCH_CODE
}).Distinct(new Reuseablecomp.SelectListItemComparer());
return objdataresult;
}
}
这是我正在使用的课程:
public static class Reuseablecomp
{
public class SelectListItemComparer : IEqualityComparer<SelectListItem>
{
public bool Equals(SelectListItem x, SelectListItem y)
{
return x.Text == y.Text && x.Value == y.Value;
}
public int GetHashCode(SelectListItem item)
{
int hashText = item.Text == null ? 0 : item.Text.GetHashCode();
int hashValue = item.Value == null ? 0 : item.Value.GetHashCode();
return hashText ^ hashValue;
}
}
}
没有返回任何内容,我收到以下错误。当我尝试没有Distinct 的基本查询时,一切正常。
{"The operation cannot be completed because the DbContext has been disposed."}
System.Exception {System.InvalidOperationException}
Inner exception = null
如何为我的下拉菜单返回不同的数据?
【问题讨论】:
标签: asp.net-mvc-4 linq-to-sql linq-to-entities