我在數據庫中有一個重複的字段。我想在下拉列表 中使用它,並且這必須返回不同的數據。用selectlistItem爲下拉列表框返回不同的數據
這是我創建的方法來做到這一點。
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;
}
}
}
什麼都沒有返回。我得到了下面的錯誤。當我嘗試了一個沒有明顯的基本查詢時,一切正常。
{"The operation cannot be completed because the DbContext has been disposed."}
System.Exception {System.InvalidOperationException}
內部異常= NULL
問:我怎樣才能返回不同的數據,我的下拉?
謝謝。我需要比較用戶名==「ndjgkd」的用戶名。我在哪裏可以在你的代碼中包含這個? – user2320476
我不明白。 「like」與「equals」(==)非常不同。這是什麼?並且,比較如何?你想要發生什麼? –
抱歉,我的意思是平等。這是一個錯字 – user2320476