Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# asp.NETC与Linq问题_C#_Asp.net_Linq - Fatal编程技术网

C# asp.NETC与Linq问题

C# asp.NETC与Linq问题,c#,asp.net,linq,C#,Asp.net,Linq,我有一个下拉列表,我正试图从数据表中提取。每行包含短日期,第二列包含月份名称,后跟年份 大概是这样的: 6/11/2011 June 2011 7/11/2011 July 2011 我试图将第二列放入下拉列表中 dateListDropDown.DataSource = DateList.Rows.Select(x=> x[1].ToString()); dateListDropDown.DataBind(); 但我认为我的Linq不正确 有人知道我做错了什么吗?

我有一个下拉列表,我正试图从数据表中提取。每行包含短日期,第二列包含月份名称,后跟年份

大概是这样的:

6/11/2011      June 2011
7/11/2011      July 2011
我试图将第二列放入下拉列表中

dateListDropDown.DataSource = DateList.Rows.Select(x=> x[1].ToString());
dateListDropDown.DataBind();
但我认为我的Linq不正确


有人知道我做错了什么吗?

假设数据不为空,尝试以下操作

 var list = new List<string>();

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

 dateListDropDown.DataSource = list;
正如您在评论中提到的,由于DataRowCollection未实现IEnumerable,因此出现错误。

请尝试以下操作:

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


是的,我有一个检查,因此如果数据为null,代码将永远不会到达这里。System.data.DataRowCollection不包含“Select”的定义,并且没有扩展方法“Select”接受类型为“System.data.DataRowCollection”的第一个参数。我指的是使用System.Linq;好的,看起来DataRowCollection没有实现IEnumerable。唯一的选择是使用简单for循环。从另一个来源获得了答案。这把它修好了。dateListDropDown.DataSource=DateList.AsEnumerable.Selectx=>x[1]。ToString;第一个按照上面评论中提到的那样工作。但既然我必须等着回答,你就可以得到它
dateListDropDown.DataSource = (from a in DateList.AsEnumerable()
                                         select a).ToList();
dateListDropDown.DataTextField = "YourSecondColumnName";
dateListDropDown.DataBind();