Jump to content

Recommended Posts

Posted

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">

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...