日期:2014-05-17  浏览次数:20611 次

GetEnumerator与foreach的问题?
C# code
    DataTable comment_dt=getComment.GetComment(topicID);
        
        StringBuilder com_str =new StringBuilder();
        foreach(var comAll in comment_dt){//遍历所有评论内容,拼接起来
            com_str.Append(comAll.Nickname);
             com_str.Append("@");
              com_str.Append(comAll.Msg);
            com_str.Append  ("#");
            com_str.Append(comAll.Greattime);     
  
 

错误提示:

“System.Data.DataTable”不包含“GetEnumerator”的公共定义,因此 foreach 语句不能作用于“System.Data.DataTable”类型的变量

我看别人的代码直接就可以遍历的?

为什么我会提示这种东西?要用GetEnumerator的话,那怎么写?

谢谢各位了


------解决方案--------------------
DataTable comment_dt=getComment.GetComment(topicID);

StringBuilder com_str =new StringBuilder();
foreach(DataRow comAll in comment_dt.Rows){//遍历所有评论内容,拼接起来
com_str.Append(comAll.Nickname);
com_str.Append("@");
com_str.Append(comAll.Msg);
com_str.Append ("#");
com_str.Append(comAll.Greattime);