Take the 2-minute tour ×

Given the premise:

  • There are competent sql programmers (correlary - writing sql queries are not an issue)
  • There are competent application developers (correlary - there is simple/strong/flexible architecture for handling connections and simple queries from code)

Why do people use linq to sql?

  • There is overhead added to each transaction
  • There is strong likelihood of performance loss for moderate-complex calculations (DBs are made for processing sets and calculations and had teams of engineers working out optimization - why mess with this?)
  • There is loss of flexibility (if you want to add another ui (non .NET app) or access method, you either have to put the queries back in the db or make a separate data access layer)
  • There is loss of security by not having a centralized control of write/update/read on db (for example, a record has changed - if you allow applications to use linq to sql to update, then you cannot prove which application changed it or what instance of an application changed it)

I keep seeing questions about linq to sql and am wondering if I'm missing something.

share|improve this question
add comment

11 Answers

up vote 36 down vote accepted


It's not that you're missing something. It's that you have something most shops don't have:

There are competent sql programmers

Additionally, in your shop those competent sql programmers prefer to write sql.

Here's a point by point response:

There is overhead added to each transaction

Generally true. This can be avoided by translating the queries before they are needed to run using CompiledQuery for many (but not all!) scenarios.


Either you're writing linq, which is translated to sql, and then a plan is generated from the optimizer - or your writing sql from which a plan is generated by the optimizer. In both cases you are telling the machine what you want and it is supposed to figure out how to do it. Are you suggesting that subverting the optimizer by using query hints is a good practice? Many competent sql programmers will disagree with that suggestion.


A lot of people using linq are already SOA. The linq lives in a service. The non .NET app calls the service. Bada-bing bada-boom.


This is simply not true. You prove which application is connected and issuing sql commands the same way you prove which application is connected and calling a sproc.

share|improve this answer
If I could upvote it more than once I would... Well written, Good answer! –  bytebender Nov 13 '09 at 22:50
+1, but I would like to add just two tiny details. 1) CompiledQuery are great for often executed queries with no optional parameters. If you have a lot of optional parameters in a query they can instead hurt performance due to the loss of the flexibility added by L2S's client-side query optimization/simplification. 2) Even for competent SQL programmers, writing strongly typed queries takes away a lot of that "glancing on the printed data model diagram on the desk" when writing complex queries. Intellisense fills out the details... –  KristoferA Aug 8 '10 at 3:49
Good points KristoferA. The other place CompileQuery doesn't help is if the query has localCollection.Contains, as the collection may vary in size. –  David B Aug 8 '10 at 13:21
add comment

Let me list you a few points:

  1. There are small software companies or mid-sized companies who develop their software in-house who might rather focus on getting many application developers than getting a freelancer DB developer or even permanently hire one.
  2. In most cases the overhead is a non-issue either due to the amount of data to be processed or due to the low traffic. Besides, when used properly, LINQ to SQL can perform as fast as most SQL queries + the associated .net code.
  3. Many companies just stick with the Microsoft stack and they can only enjoy the integration. Some other company develops using SOA there's just no problem. The others aren't forced to choose LINQ-to-SQL and if they make that choice is their problem how to integrate it. Nobody ever said LINQ-to-SQL is a silver bullet :)
  4. I believe security is gained with LINQ-to-SQL because I've bumped across lots of SQL queries taking in unescaped data with string concatenation etc and explaining the whole parametrized query idea has never been easy. Besides since all queries are eventually translated into SQL, unless the tracking issue you describe would happen via a stored procedure, there're again no problems at all.

I also believe your question can be posed more generally to address all ORMs and not just LINQ-to-SQL, and still most of what I said would hold true.

share|improve this answer
Perfect answer. +1 –  unforgiven3 Jun 19 '09 at 17:39
A good app developer should be able to write intermediate level SQL, especially for LOB apps that are typically heavily db dependent. Also, I'd say you can't write decent LINQ to SQL without understanding what its doing under the hood, including the SQL it is writing. –  alchemical Jul 2 '09 at 19:11
I beg to differ. In fact while generic principles of relational databases and querying should be known in order to write good code, SQL is just a (bad) dialect and hiding it behind a safer, more intuitive layer is something that should be hailed to say the least. Would you want to prove me wrong, you should be able to defend the idea of somebody learning inner and outer joins in SQL and why can't one be a good developer accessing databases without distinguishing that. Or any of the many peculiarities related to SQL that one can live without. –  emaster70 Jul 2 '09 at 21:19
add comment

The problem is that it is very rare for somewhere to have a competent SQL developer who likes writing SQL and wouldn't rather be doing something else. I would consider myself competent in SQL, I used to do all my data access layers with stored procs or parametrized queries. Trouble is that it takes ages and is dull. I'd rather be writing great applications than messing around with data access layers that essentially have a select, insert, update and delete SQL statement(or proc) repeated dozens of times for each data object.

Linq-to-SQL takes away some of the repetitive nature. It has a tool to auto generate you business objects from your database schema, and it gives you a nice integrated query language that is compile time type verified and is in your code (Stored procs are a pain to source control neatly)

I can write a DAL in Linq-to-sql several times faster than I can using plain SQL, stored procs or parametrized queries.

If you want to maintain the use of stored procs both linq-to-sql and the EF both support the use of stored procs for all their data access, you just have to set up the appropriate mappings. So, you can still use your stored procs to log details and implement security if you want. We tend to opt for using windows auth, and use that to restrict access to each table for the various users, then we have a bunch of triggers on the tables that track details for audit purposes.

Two things I will quickly note is that firstly, the entity framework seems to be getting more support from MS at the moment, and I suspect that will be considered the kind of default standard for the future in preference to linq-to-sql. Secondly, in .Net 3.5 the EF and linq-to-sql do not have very good support for n-tier disconnected apps. In both of them you kind of have to muck around with either serializing data contexts across your disconnected tiers, or manually detach and re-attach your data objects. This is much improved in the .net 4.0 though. Just something to consider depending on which version you have available to you.

share|improve this answer
add comment

Existing question/answers in the same vein/spirit:

I personally believe there's no right or wrong answer. It depends on what you're developing and how you're developing it. If you need razor-sharp performance, have an overly-complex data model, etc... skip the abstraction. If you feel the abstraction speeds up your development time, like the idea of capturing all application logic in a single codebase, etc... use it.

share|improve this answer
thanks for posting the links! –  mson Jun 19 '09 at 15:02
add comment

For me, it takes a lot less time to write linq to sql code than it does to write a bunch of stored procedures. That's especially true when the design isn't finished, in that case I don't yet know how much of the work I want to do on C# objects, and how much I want to do in SQL.

So, I can skip building datasets, I don't have to click click click to add queries, basically, linq to sql means I can change my code in less time.

Also, as a big fan of Haskell, I can write lots of functional-style code with linq to sql and it just works.

share|improve this answer
add comment

I'm not saying this is an ideal solution or even a great example (it was the result of a high level constraint on the architecture, not something we necessarily would have chosen from scratch), but...

I worked on an app where the code was completely isolated from the database except through a set of exposed stored procs. The code could not "know" anything about the database schema except was was returned from the stored procs.

While this isn't that unusual and it isn't too hard to write a DAL using ADO or whatever, I decided to try out Linq to Sql, even though it wouldn't be using it for its real intended purpose and wouldn't use most of the features. Turns out it was a great decision.

I created the Linq to Sql class, dragged the stored procs from server explorer onto the right side of the designer, then... Wait, there is no then. I was pretty much done.

Linq created strongly typed methods for each stored proc. For the procs that returned rows of data, Linq automatically created a class for the items in each row and returned a List<generatedClass> for them. I wrapped the calls themselves in a lightweight public DAL class that did some verification and some automatic parameter setting and I was done. I wrote a business object class and mapped the dynamically generated Linq class objects to the business object (did this by hand, but it isn't hard to do or maintain).

The program is now immune to any schema change that doesn't affect the stored procedure signatures. If the signatures do change, we just drag off the old proc from the design and drag it back to regenerate the code. A few passes through the unit tests to make changes (which usually don't go higher than the public DAL interface) and it's done. Things upstream of the DAL use Linq to Objects techniques to select, filter, and sort data that isn't in the right format straight from the stored proc calls.

We have some excellent DBAs writing the stored procedures and an entirely different group writing the other code, so maybe it is a good example of why (and how) you can use LINQ in the scenario you describe.

share|improve this answer
add comment

Some handy features are the debugger picking up sytax errors in your query, compared to writing SQL statements as strings. Mistakes that wont get picked up until runtime.

Plus I find LINQ statements easier to read than SQL.

share|improve this answer
whoa... no one is allowed to write sql statements as strings. also, the sql server debugger is just as good as the vs debugger –  mson Jun 19 '09 at 13:53
@mson: The sql server debugger doesn't pick up when your program tries to store a int value in a varchar field. Linq would - at compile time. –  Simon P Stevens Jun 19 '09 at 13:59
@mson: some people do believe it or not. Even if you are using the SQL ADO.NET classes you can still have problems with procedures etc missing parameters etc. –  Chalkey Jun 19 '09 at 14:31
add comment

It may be a case of convenience triumphing over performance. If you're programming at Facebook levels of uber-performance then you might think about every clock cycle but the simple truth is that the majority of applications don't need this attention and benefit from efficiencies in code maintenance and dev time (100k contractor vs. another $erver).

That said, there's a case for outsourcing as much of the query processing from the DB box in very high scale systems, else the DB is the bottle neck and you need to shard or re-architect down the line. Costly.

I think its fair to say that LINQ will scale better/easier both in terms of servers and from many core in that your LINQ codebase will get m-core for 'free' as soon as MS release C# 4.0.

I do see your point in asking and as a non-ASP.NET dev just beginning a www project for the first time, I can't see the point of 80% of ASP.NET (themes, controls etc.) - it seems I need to learn more and code more than the HTML itself! -- again, I'm sure there's an good reason for it all.

--- I haven't got the 50 pts to comment on the post I want to so I'm doing it here ---

David B suggests that writing some SQL is all there is to getting the most out of SQL Server and that using query hints is the steering mechanism. The same task can be achieved in many different ways in SQL and many with 1000s of times the performance gain. I suggest reading Inside T-SQL Querying (Itzik Ben Gan). Over and over, Itzik shows how to rethink the query and use new commands to shrink the logical reads sometimes from thousands into less than ten.

share|improve this answer
add comment

For very simple queries, the overhead of an extra layer adds to the roundtrip cost. For somewhat more complex queries in normal 'business app' scenarios, the optimizations done by the Linq-to-SQL expression->sql translation magic can often save a lot.

As an example, I recently did a 1:1 translation of a customer-supplied 1400+ (!) line stored proc to L2S. Not only did it go from 1400 lines of SQL to 500 lines of much more readable, strongly typed, and commented code. It also started hitting the database with an average of ~1500 reads instead of ~30k reads. This is before I even started looking at db-side optimizations - that saving is something I can 100% attribute to L2S's ability to eliminate predicates that can be evaluated client-side.

share|improve this answer
i have a feeling you could have gotten an even better improvement by optimizing the code right in sql... –  mson Jun 19 '09 at 19:52
Yes, but it would have taken a much larger effort. Here it was done for me by the framework - saving man-hours. –  KristoferA Jun 20 '09 at 3:05
add comment

simple answer, there are two approaches: create exquisite Rube Goldberg contraptions, or just get the job done in a simple way. Many devs lean towards the former.

Developers get bored easily, and would often personally enjoy doing things a harder way that seems to provide a certain intellectual beauty. Are you developing an app or writing a PhD? As my msft director used to yell in the hallways, "I don't want another research project!"

share|improve this answer
add comment

please repeat after me (min 3x)

  • there is no silver bullet

  • I will not use a technology just because its the latest thing from msft

  • I will not use something just to get it on my resume

Not only are their competent SQL coders, any decent app programmer, especially LOB apps, should write intermediate SQL. If you don't know any SQL and are writing LINQ to SQL, how are you going to debug your data calls? How are you going to profile them to fix bottlenecks?

We're trying out LINQ to SQL and I think there are major issues with it, such as:

  • There is no simple way to return the query results to another object. This in itself seems insane. Microsoft created the var anonymous datatype, and recommends using it, but there is no way to move this data out of your local method, hence the oo paradigm breaks if you have to use the data in the same function that retrieved it.

  • Tables are NOT objects. Study up on 3rd normal form etc. Relational databases are for storing data, not using it. I don't want to be restricted or encouraged to use my tables as objects. The data I retrieve from the database will very often be joins of multiple tables, and may include SQL casts, functions, operators, etc.

  • There is no performance gain, and a slight loss

  • Now I have way more code to worry about. There are the dbml files and still a DAL to actually write the LINQ. Yes, lots of it is machine-generated, that doesn't mean its not there, its something else that can go wrong (i.e. your dbml files, etc.).

Now that I've given the background, I will attempt to answer you actual question, why do people use LINQ To SQL:

  • Its the latest thing from Microsoft and I want it on my resume.
  • Msft has convinced managers/execs that it will decrease coding time
  • Developers hate SQL. (no good dev environment or debugging except manually--it would be nice to have better intellisense to a sql tool.)

I encourage people not to jump on the bandwagon just because everyone else is, learn enough to put it on your resume, be willing to use it if forced to, but try and really understand the pros and cons first.

share|improve this answer
add comment

Your Answer


