首页 文章

LINQ超过数据集错误

提问于
浏览
4

我正在尝试使用以下代码从数据集中检索数据:

var all_pepole = from rows_of_bank in ds1.Tables[0].Rows select rows_of_bank;
    foreach (System.Data.DataRow row in all_pepole)
    {
        Console.WriteLine("{0} {1} is {2} years old.", row[0].ToString(), row[1].ToString(), row[2].ToString());
    }

但是这个代码会给我带来错误,这是错误的:

Could not find an implementation of the query pattern for source type 'System.Data.DataRowCollection'. 'Select' not found. Consider explicitly specifying the type of the range variable 'rows_of_bank'

1 回答

  • 9

    ds1.Tables[0].Rows 的类型为 DataRowCollection ,它实现 IEnumerable 但不实现 IEnumerable<DataRow> . 大多数Linq运算符仅适用于通用接口 . 您可以将项目转换为 DataRow ,如下所示:

    var all_pepole = from rows_of_bank in ds1.Tables[0].Rows.Cast<DataRow>() select rows_of_bank;
    

    或者您可以使用Linq中的 AsEnumerable 扩展方法到DataSet:

    var all_pepole = from rows_of_bank in ds1.Tables[0].AsEnumerable() select rows_of_bank;
    

相关问题