DataSet From linq

Joined
Jan 10, 2007
Messages
43,898
Location
In The Machine
So I have some Linq code that returns some IQueryable.

Now I want to take the query and return a DS (for some legacy systems that I dont have time to rewrite at the moment, but would still like to leverage the a common Linq framework).

If the Linq is very basic, it is trivial to get a DataSet back, just use an extension method like:

publicstaticDataSet LinqDS(thisIQueryable mySource)
{
***DataSet ds = newDataSet();
***using (SqlConnection connection = newSqlConnection(_connectionString))
***{
******using(SqlCommand command = newSqlCommand(mySource.ToString()))
******{
*********command.CommandType =
CommandType.Text;
*********command.Connection = connection;
*********connection.Open();
*********SqlDataAdapter da = newSqlDataAdapter(command);
*********da.Fill(ds);
*********connection.Close();
******}
******return<font size=2> ds;
***}
}

Call it by something like:var o = from*d in DB.MyTable
******select d;

DataSet
ds = o.LinqDS();

But if the Linq contains a where, this extension method won't work because of the way that Linq generates the sql query.

var o = from*d in DB.MyTable
***where d.Column1 > 0
******select<font size=2> d;

Would generate SQL like:

SELECT [t0].[Column1], [t0].[Column2]
FROM [MyDB].[dbo].[MyTable] AS [t0]
WHERE [t0].[Column1] <font color="#808080" size=2 face="Courier New"><font color="#808080" size=2 face="Courier New"><font color="#808080" size=2 face="Courier New">
 
Back
Top