我正在尝试使用LINQ,因此可以将其绑定到ASP.NET下拉列表。
至今; 这就是我在LINQ表达式方面的表现;我是LINQ的新手,我从今天早上开始学习它。
county.DataSource = (from taxUnit in TaxUnits.Descendants("CodeData")
join taxType in TaxTypes.Descendants("CodeData")
on
taxUnit.Attribute("taxuntype").Value
equals
taxType.Attribute("codeid").Value
where taxType.Attribute("taxuntypky").Value == "CO"
select new
{
County = taxUnit.Attribute("desc"),
CodeID = taxUnit.Attribute("codeid")
});
county.DataTextField = "desc";
county.DataValueField = "codeid";
county.DataBind();
最初,我试图将其转换为数据表。我们已经得知您实际上可以直接将LINQ绑定到DropDownList。
<asp:Label AssociatedControlID="county" runat="server">County</asp:Label>
<asp:DropDownList
ID="county"
runat="server" />
到目前为止,结果是...当我查看下拉框时。根据我从LINQPad获得的信息,我期望
County CodeID
Willow County 1
CP2 TU 2
当前答案的唯一问题是我不知道它如何工作,因此我不明白如何将其转换为我想要的东西。
以前,我可以使用指向此方法的ObjectDataSource,并为List制作了一个模拟文件,以便与数据绑定一起使用。
如果实际的问题是-如何将LINQ与XML结果绑定到DropDownList上,那么这是我以前的ASP.NET项目中的代码。我将LINQ绑定到实体结果到GridView上。对于DropDownList应该以相同的方式工作:
public IEnumerable<IDraft> Get(int accountId, DateTime startDate, DateTime endDate)
{
using (var db = new ModelContainer())
{
var account = db.Accounts.SingleOrDefault(a => a.ID == accountId);
return (from d in account.Drafts
let date = d.Date.Date
where startDate.Date <= date && date <= endDate.Date
orderby d.Date ascending
select d).ToArray();
}
}
protected void gridView_OnDataBinding(object sender, EventArgs e)
{
((IDataBoundControl)sender).DataSource = IoC.Resolve<IOperationRepository>().GetShared(this.ObjectId.Value, ucChooseDate.StartDate, ucChooseDate.EndDate);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句