此代码的最后两行无法正常工作 - 结果将从LINQ查询返回。我只是不确定如何成功地将结果中指示的列绑定到下拉列表的textfield和valuefield:
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();
}
答案 0 :(得分:33)
protected void BindMarketCodes()
{
using(var dc = new LINQOmniDataContext())
{
dd2.DataSource = from p in db.lkpMarketCodes
orderby 0
select new {p.marketName, p.marketCodeID };
dd2.DataTextField = "marketName";
dd2.DataValueField = "marketCodeID";
dd2.DataBind();
}
}
// no need to use ToList()
// no need to use a temp list;
// using an anonymous type will limit the columns in your resulting SQL select
// make sure to wrap in a using block;
答案 1 :(得分:29)
见下面的修订代码
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();
}
}
答案 2 :(得分:-3)
DropDownList ddl_RouteLocation = (DropDownList)e.Row.FindControl("ddl_RouteLocation");
ddl_RouteLocation.DataSource = dtLocation;--(dtlocation i have return method of linq in dtlocation)
ddl_RouteLocation.DataTextField =dtLocation.Rows[0]"LocationName"].ToString();
ddl_RouteLocation.DataValueField =dtLocation.Rows[0]["LocationId"].ToString();
ddl_RouteLocation.DataBind();
ddl_RouteLocation.Items.Insert(0, new ListItem("--Select--", "0"));