该代码的最后两行无法正常工作-结果从LINQ查询返回。我只是不确定如何将结果中的指示列成功绑定(bind)到下拉列表的文本字段和值字段:
protected void BindMarketCodes()
{
List<lkpMarketCode> mcodesList = new List<lkpMarketCode>();
LINQOmniDataContext db = new LINQOmniDataContext();
var mcodes = from p in db.lkpMarketCodes
orderby 0
select p;
mcodesList = mcodes.ToList<lkpMarketCode>();
//bind to Country COde droplist
dd2.DataSource = mcodesList;
dd2.DataTextField = mcodesList[0].marketName;
dd2.DataValueField = mcodesList[0].marketCodeID.ToString();
}
最佳答案
参见下面的修订代码
protected void BindMarketCodes()
{
using (var dataContext = new LINQOmniDataContext()) {
//bind to Country COde droplist
dd2.DataSource = from p in dataContext.lkpMarketCodes
orderby p.marketName
select new {p.marketCodeID, p.marketName};
dd2.DataTextField = "marketName";
dd2.DataValueField = "marketCodeID";
dd2.DataBind();
}
}