Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ Over a Dataset Error

I am trying to retrieve data form a dataset by using code below :

    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 this codes will raise error for me and this is error :

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'

like image 999
S.A.Parkhid Avatar asked Jan 19 '23 05:01

S.A.Parkhid


1 Answers

ds1.Tables[0].Rows is of type DataRowCollection, which implements IEnumerable but not IEnumerable<DataRow>. Most Linq operators work only on the generic interface. You can cast the items to DataRow like this:

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;
like image 66
Thomas Levesque Avatar answered Jan 25 '23 18:01

Thomas Levesque