views:

34

answers:

1

how to extract DataTable or DataSet from Linq Query or List. e.g I have linq query like this

MyDBpDataContext dcSp = new MyDBpDataContext(); dcSp.myProgrammSP().ToList();

I wrote an Sp which sends a Table as result and I have code which is already using DataTable So I want to convert this result in DataTable.

+1  A: 

The result doesn't exist as a DataTable at any point, so if you want it as a DataTable, you have to create one and copy the data into it.

Example:

DataTable table = new DataTable("Items");
table.Columns.Add(new DataColumn("Id", typeof(Int32)));
table.Columns.Add(new DataColumn("Name", typeof(String)));
foreach (Item item in items) {
  DataRow row = table.NewRow();
  row["Id"] = item.Id;
  row["Name"] = item.Name;
  table.Rows.Add(row);
}
Guffa
@Guffa: so you mean that we can convert the result into DataTable from List through DataAdapter
Azhar
@Azhar: I looked a bit into use a DataAdapter, but as far as I see that's not really practical as you have to implement a layer that reads the data and serves it to the adapter. I added an example on how to create a DataTable directly, which is easier.
Guffa