Understanding LINQ to SQL Query Translations

I posted two blog posts last weekend about using LINQ to easily perform data access against a database.  Several people have asked me for more details on LINQ to SQL since then.

Sahil Malik made some excellent posts earlier this summer that go into more detail about LINQ to SQL (aka DLINQ) and how it works.  You can read his posts here:

Bill Wagner has also recently made several great posts that go into more detail about how LINQ queries are translated and executed as SQL.  You can read his posts here:

As I talked about in this recent blog post, one of the really cool things about LINQ is that you can always hover over a LINQ query while in the debugger to drill in and see the underlying SQL query that is ultimately executed:

You can then expand the debug visualizer to both see and execute the underlying SQL query while in the debugger:

This provides a really cool way to see what is happening under the covers and to easily experiment and adapt your queries however you want. 

Hope this helps,

Scott

Published Friday, September 01, 2006 10:26 PM by ScottGu
Filed under: , , , ,

Comments

# re: Understanding LINQ to SQL Query Translations

Saturday, September 02, 2006 8:44 AM by vikram
To be able to look at the query is a great stuff

# re: Understanding LINQ to SQL Query Translations

Saturday, September 02, 2006 10:57 AM by Sahil Malik
Yeah this was added in the May CTP. IMO this is very useful. It would be rather nice if this visualizer was extended to display execution plan as well. Of course what happens in this space is highly dependent on where ADO.NET eF goes versus LINQ to SQL. I'm of course Curiously waiting .. :) SM

# Link Listing - September 2, 2006

Saturday, September 02, 2006 11:55 PM by Christopher Steen
Mono 1.1.7 and NUnit [Via: Jamie Cansdale ] Build it quickly, use it as soon as possible, and make it...

# re: Understanding LINQ to SQL Query Translations

Sunday, September 03, 2006 1:34 AM by Majid Shahabfar
Thanks Scott, If I wanna use LINQ in my ASP.NET project, should LINQ be installed on the server or it is sufficient to copy the required dll files into the bin folder?

# re: Understanding LINQ to SQL Query Translations

Sunday, September 03, 2006 7:09 AM by champs
im sorry to ask such simple question about i test LINQ with my VS2005 pro edition. i just only installed C# library. my problem is cannot find the LINQ project as you showed us. appwiz.cpl told me i had aleady installed the LINQ MAY 2006 download from LINQ default page.is it VS2005 edition problem? thx.

# re: Understanding LINQ to SQL Query Translations

Sunday, September 03, 2006 11:51 AM by ScottGu

Hi Champs,

To use LINQ you'll need to install the May CTP release of it.  This installs on top of VS 2005.

Hope this helps,

Scott

# re: Understanding LINQ to SQL Query Translations

Sunday, September 03, 2006 11:55 AM by ScottGu

Hi Majid,

The easiest way is to install LINQ on your server as well.  It uses new compilers at compile-time -- which you'll need to make sure are installed if you are using a VS 2005 Web Site Project.

One alternative would be to use a VS 2005 Web Application Project.  In this case you will need the compilers installed on your development machine, but not on the server.  Instead you can simply copy the LINQ dlls as part of the \bin directory of your application.

Hope this helps,

Scott

# re: Understanding LINQ to SQL Query Translations

Sunday, September 03, 2006 7:41 PM by Dmitriy Belchenko
Hi Scott, Do you know when a final release of LINQ is coming?

# re: Understanding LINQ to SQL Query Translations

Tuesday, September 05, 2006 12:16 AM by Seth
Hi Scott, Can you use LINQ together with AJAX in same project?

# re: Understanding LINQ to SQL Query Translations

Tuesday, September 05, 2006 2:08 AM by ScottGu

Hi Seth,

Absolutely.  LINQ and Atlas go together really, really well. :-)

Hope this helps,

Scott

# re: Understanding LINQ to SQL Query Translations

Tuesday, September 05, 2006 2:10 AM by ScottGu

Hi Dmitiy,

The final release of LINQ will come next year.  We are also looking to have a "go live" license that you can use to deploy apps with it before then.

Hope this helps,

Scott

# re: Understanding LINQ to SQL Query Translations

Tuesday, September 05, 2006 2:17 AM by ScottGu

Hi Anubhav,

Lots of people like to debate SPROCs vs. embedded SQL in the middle-tier.  The good news is that LINQ supports both options.  So if you prefer SPROCs, you can use them to perform LINQ queries against (check out Sahil's articles above for you this works).  

Hope this helps,

Scott

# re: Understanding LINQ to SQL Query Translations

Tuesday, September 05, 2006 12:08 PM by Ethan
Scott, what is your take on the SPROC vs. SQL debate?

# LINQ/DLINQ: Feasibility as an Enterprise Architecture Solution

Wednesday, September 06, 2006 12:24 PM by Bohemian
Am I missing something obvious? It seems that LINQ/DLINQ is being proposed as a replacement for T-SQL/PL-SQL, Stored Procedures & or Web Services… From what I see so far LINQ/DLINQ looks like a good idea for RAD (Rapid Application Development). As well as useful for those who don’t know how & or want to write T-SQL/PL-SQL stored procedures and or web services with or without the .NET framework and providers. Moreover, reap the benefit of a RDBMS with over a decade of query optimization and other functionality not available as far as I can tell thus far with DLINQ/LINQ. Will the SQL Server or Oracle query optimizers be able to cache and or store what it interprets as dynamic SQL coming in from DLINQ/LINQ statements? In my opinion, it does not look like a good idea for Scalable Enterprise Application Architecture. Do we really want to recompile our application every time we want to make a change to our DLINQ/LINQ query(s)/statement(s)? How would we fit DLINQ/LINQ into SOA (Service Orientated Architecture)? Is it not a better design pattern to keep the data access, manipulation etc., separate from the application it self? What about large complex SQL queries? What about SQLXML, Bulk XML Inserts etc., ? What about SQL performance tuning and monitoring? Seems like DLINQ/LINQ is attempting to take veteran DBA’s out of the picture… Please don’t take this as a rant; this is just some initial observations of the CTP of DLINQ/LINQ that I have some serious concerns about. If I have overlooked something obvious or otherwise or you have something further to add, comment suggest etc., I would definitely welcome it and may influence the decisions of other developers, dba’s, managers etc., as to whether or not DLINQ/LINQ is a viable, scalable, enterprise application, architecture solution, design patter etc. Best Regards.

# re: Understanding LINQ to SQL Query Translations

Thursday, September 07, 2006 3:45 AM by ScottGu

Hi Ethan,

I think there are pros and cons to using SPROCs vs. SQL directly.  Sprocs have some advantages in that they can be locked down and secured tighter, and they provide the ability to better control the SQL that executes against your database (which can sometimes lead to better perf if done right).

Having said that, object relational mappers like LINQ (and others) really do provide a ton of flexibility and productivity that is hard to ignore.  They also make it easier to model your business entities and logic.  

Hope this helps,

Scott

# re: Understanding LINQ to SQL Query Translations

Thursday, September 07, 2006 4:00 AM by ScottGu

Hi Bohemian,

I wouldn't think of LINQ as a RAD feature (meaning designer/wizard focused).  Rather I think of LINQ as a way to radically improve the expressiveness and query capabilities of ORMs (object relational mappers).

The benefit of an ORM approach to data is that it allows you to model your business entities and logic much better, and helps break down some of the impedence mismatch that exists today between rows in a database and objects in the middle-tier.  As such, technologies like LINQ can make a huge improvement to enterprise applications and solutions.

Note that one of the benefits of LINQ is that it still allows you to drop down and execute custom queries or call SPROCs directly if you want - without loosing most of the big benefits of LINQ.  So in cases where you/want need to-do this you can still do so.

Hope this helps,

Scott

# re: Understanding LINQ to SQL Query Translations

Thursday, September 07, 2006 9:03 AM by Bohemian
RE: "impedence mismatch that exists today between rows in a database and objects in the middle-tier." I do not quite follow this statement the definition of impedance is as follows... Impedance: The apparent resistance in an electric circuit to the flow of an alternating current, analogous to the actual electrical resistance to a direct current, being the ratio of electromotive force to the current. It is equal to R2 + X2, where R = ohmic resistance, X = reactance. For an inductive circuit, X = 2/fL, where f = frequency and L = self-inductance; for a circuit with capacity X = 1 / 2/fC, where C = capacity. Could you elaborate on what sounds like a LINQ marketing statement in "Impedence mismatch that exists today between rows in a database and objects in the middle-tier." Thanks in advance.

# re: Understanding LINQ to SQL Query Translations

Tuesday, September 12, 2006 11:01 AM by ScottGu

Hi Bohemian,

By impedence mismatch, I mean that when you retrieve data from a database you get back a row object containing some raw relational data, but loose many of the semantics that the data has in the database.  When you then work on it in an object oriented world it becomes further disconnected from the underlying data model, and when you update it you need to write a lot of custom code to do things right.

The nice thing about LINQ (and other O/R approaches) is that it preserves the underlying data semantics without you having to write any custom code to make this happen.

Hope this helps,

Scott

# re: Understanding LINQ to SQL Query Translations

Friday, September 15, 2006 7:54 PM by Erik
Hi Scott, You mentioned earlier that Linq will officially release 'next year' -- do you know approximately when? I'm trying to convinve my partners to put off a project until Linq releases, and I'd like to have some idea when the final release will be available. Any degree of granularity is fine - I just need something to *say,* if you know what I mean. =) Thanks a bunch, Erik

# re: Understanding LINQ to SQL Query Translations

Tuesday, September 19, 2006 12:17 AM by ScottGu

Hi Erik,

My guess is that the final release will be in the second half of next year -- although that is very speculative.  We will also probably look to have a go-live license at somepoint before that if you wanted to go live in production prior to the final release.

Hope this helps,

Scott

# re: Understanding LINQ to SQL Query Translations

Friday, September 22, 2006 8:22 AM by Mads
How is the intellisence support in the current version? I tried out LINQ a while ago, but the inntellisence was kinda buggy.

# re: Understanding LINQ to SQL Query Translations

Friday, September 22, 2006 3:11 PM by ScottGu

Hi Mads,

The intellisense support in the CTP is not there (it instead uses the old VS 2005 intellisense engine for C# -- which doesn't recognize the new keywords yet).  

The real intellisense will start to show up early next year.

Hope this helps,

Scott

# LINQ, DLINQ, XLINQ - .net 3.0

Monday, October 09, 2006 2:43 AM by Abu Ismail - Loves .NET & "Arif"

• DLINQ - Sahil Malik http://blah.winsmarts.com//2006/06/02/demystifying-dlinq-part1--an-introduction-to-dlinq.aspx

# LINQ / DLINQ resource...

Tuesday, October 31, 2006 4:09 AM by Rexiology::Work

Just read from ScottGu's blog post that included useful links to LINQ / DLINQ . just leave a...

# LINQ / DLINQ resource...

Tuesday, October 31, 2006 4:12 AM by Rexiology...

crosspost from http://rextang.net/blogs/work/ Just read from ScottGu's blog post that included useful

# Visual Studio Orcas

Tuesday, December 19, 2006 8:21 AM by Ngoc Luu

When will LINQ release?

# re: Understanding LINQ to SQL Query Translations

Tuesday, December 19, 2006 12:56 PM by ScottGu

Hi NGoc,

LINQ will ship as part of the "Orcas" release of Visual Studio and the .NET Framework.  We are currently planning to ship this the second half of 2007.

Thanks,

Scott

# re: Understanding LINQ to SQL Query Translations

Wednesday, December 20, 2006 11:23 AM by Ngoc Luu

Is there an "Orcas" version of .Net Framework? So, what is the difference between that version of .Net Framework with the current version 3.0?

# re: Understanding LINQ to SQL Query Translations

Wednesday, December 20, 2006 12:15 PM by ScottGu

Hi Ngoc,

There will be an "Orcas" version of the .NET Framework coming.  This will be the version after .NET 3.0.

Thanks,

Scott

# LINQ with SQL Server Compact (a.ka. DLINQ with SQL CE)

Tuesday, August 21, 2007 4:28 AM by SQL Server Compact - Compact yet capable

Querying data from SSC database gets easier! “ LINQ ” stands for .net L anguage IN tegrated Q uery. LINQ-enabled

# MSDN Blog Postings » LINQ with SQL Server Compact (a.ka. DLINQ with SQL CE)

Pingback from  MSDN Blog Postings  » LINQ with SQL Server Compact (a.ka. DLINQ with SQL CE)

# Want to make life easier developing databases apps against SQL, XML and other databases (Oracle, MySQL, PostgreSQL) - look at the Beginning LINQ development articles

Friday, January 18, 2008 1:03 PM by David Overton's Blog

LINQ looks like another amazingly simple move for app developers (I've written a few DB apps myself

# LINQ with SQL Server Compact (a.k.a. DLINQ with SQL CE)

Sunday, February 24, 2008 5:12 PM by Pocket.Info - Mobile 2.0

Querying data from SSC database gets easier! “ LINQ ” stands for .net L anguage IN tegrated Q uery. LINQ