Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Data.DataRowCollection' does not contain a definition for 'Cast'

Tags:

c#

I am stuck here , the following method shows error

'System.Data.DataRowCollection' does not contain a definition for 'Cast' and the best extension method overload 'System.Data.EnumerableRowCollectionExtensions.Cast(System.Data.EnumerableRowCollection)' has some invalid arguments

How to resolve this I tried datatable.AsEnumerable but wont work. The bold lines in the code shows this error. Help me please.

Here is the code

   public void DataExport(string SelectQuery)// server code
   {
       try
       {
           using (var dt = new DataTable())
           {
               using (var da = new SqlDataAdapter(SelectQuery, con))
               {
                   da.Fill(dt);
                   var rows =
                       **from dr in dt.Rows.Cast<DataRow>()**
                       select String.Join(
                           ",",
                           **from dc in dt.Columns.Cast<DataColumn>()**
                           let t1 = Convert.IsDBNull(dr[dc]) ? "" : dr[dc].ToString()
                           let t2 = t1.Contains(",") ? String.Format("\"{0}\"", t1) : t1
                           select t2);



                   using (var sw = new StreamWriter("somepath"))
                   {
                       // sw.WriteLine(header);
                       foreach (var row in rows)
                       {
                           sw.WriteLine(row);
                       }
                       sw.Close();
                   }
               }
           }
       }
       catch (Exception e) { Console.WriteLine(e.Message); }
   }
like image 378
Member123 Avatar asked Jun 01 '15 10:06

Member123


Video Answer


1 Answers

You have to add System.Linq reference to your file.

using System.Linq;

Enumerable.Cast Method

like image 51
Pawel Maga Avatar answered Oct 11 '22 18:10

Pawel Maga