当前位置:  开发笔记 > 编程语言 > 正文

如何将LINQ数据绑定到下拉列表

如何解决《如何将LINQ数据绑定到下拉列表》经验,为你挑选了2个好方法。



1> andleer..:
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;



2> James..:

请参阅下面的修订代码

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();
    }
}


在我添加dd2.DataBind()之前,它无法正常工作; 正如#Andrew_Robinson所建议的那样
推荐阅读
N个小灰流_701
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有