简体   繁体   English

asp.net C#和Linq问题

[英]asp.net C# and Linq issue

I have a DropDown list I am trying to pupulate from a DataTable. 我有一个DropDown列表,我试图从DataTable中删除。 Each row contains the short date, and the second column contains the month name followed by the year. 每行包含短日期,第二列包含月份名称,后跟年份。

Something like this: 像这样:

6/11/2011      June 2011
7/11/2011      July 2011

I am trying to throw the second column into a drop down list. 我正在尝试将第二列放入一个下拉列表。

dateListDropDown.DataSource = DateList.Rows.Select(x=> x[1].ToString());
dateListDropDown.DataBind();

But I don't think I have the Linq correct. 但我认为Linq正确。

Can anyone see what I am doing wrong here? 有人可以在这里看到我做错了吗?

try following (assuming data is not null) 尝试跟随(假设数据不为空)

 var list = new List<string>();

 foreach(var row in DateList.Rows)
   list.Add(row[1] as string);

 dateListDropDown.DataSource = list;

As you have mentioned in comments, the error is coming as DataRowCollection does not implement IEnumerable. 正如您在评论中提到的那样,由于DataRowCollection没有实现IEnumerable,因此出现了错误。

Try this: 尝试这个:

dateListDropDown.DataSource = DateList.AsEnumerable().Select(x => x[1].ToString());
dateListDropDown.DataBind();

OR 要么

dateListDropDown.DataSource = (from a in DateList.AsEnumerable()
                                         select a).ToList();
dateListDropDown.DataTextField = "YourSecondColumnName";
dateListDropDown.DataBind();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM