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); }
}
You have to add System.Linq
reference to your file.
using System.Linq;
Enumerable.Cast Method
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With