LINQ for dataset error

I am trying to get data from a dataset using the following code:

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()); } 

But these codes will raise an error for me, and this is an error:

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

+4
source share
1 answer

ds1.Tables[0].Rows is of type DataRowCollection , which implements IEnumerable but not IEnumerable<DataRow> . Most Linq operators only work with a common interface. You can pass DataRow elements as follows:

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

Or you can use the AsEnumerable extension method from Linq to DataSets:

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

Source: https://habr.com/ru/post/1382669/


All Articles