linq with sql..to get a dataset :)

http://www.west-wind.com/WebLog/ShowPost.aspx?id=141435

 

When using LINQ to SQL, it lets you retrieve entity lists as results from SQL queries. While that’s usually what you would like to retrieve from LINQ query, LINQ to SQL also provides an alternative to retrieve a raw IDbCommand statement with the SQL command and all parameters set based on the query expression.

This means that you can actually bypass the Entity list generation and use the query for other purposes like generating a DataReader instead:

var q = from c in db.tt_customers
        where c.Pk > 5
        select c;                        
 
IDbCommand command = db.GetCommand(q);
command.Connection = Customer.context.Connection;
command.Connection.Open();
IDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
 
this.dgGrid.DataSource = reader;
this.dgGrid.DataBind();
 
 
 
 
Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s