Take the 2-minute tour ×

What are the fundamental misunderstandings people have when they first start using LINQ?

For instance, do they think it is one thing when it is really something else?

And, are there some best practices to employ to avoid these mistakes?

share|improve this question
Do you mean LINQ -or- LINQ to SQL? :-) –  Aaron Hoffman Apr 22 '09 at 14:49
Either or both :) –  Even Mien Apr 22 '09 at 14:55
I use the :-) because I think that is one of the biggest misconceptions people have about LINQ; that it is only LINQ to SQL. –  Aaron Hoffman Apr 22 '09 at 15:48

15 Answers 15

up vote 56 down vote accepted

That it should be used for everything.

share|improve this answer
I'd up-vote this twice if I could! –  Inisheer Apr 22 '09 at 14:13
It's certainly important to recognise when it isn't the right approach. –  Jeff Yates Apr 22 '09 at 14:21
I see lots of programmers who overuse implicitly typed local variables. –  RichardOD Apr 30 '09 at 14:35
Can you give an example of when it shouldn't be used? I guess I'm still in the "use it whenever possible" stage. –  Joe Chung Jul 3 '09 at 6:35
@JTA you can accomplish that by either unvoting and upvoting this answer again, or you can downvote all the rest of the answers to get the same effect :P –  nawfal Nov 21 '12 at 14:16

Failing to understand the differences betweeen (or existence of!):


Not understanding deferred execution.

share|improve this answer
+1 for noting Single. I've been using First and commenting that "// There can be only one". –  Greg D Apr 22 '09 at 14:39
Yeah- deferred execution got me when I first started to use LINQ. I had a unit test that was generating different test data when I expected it to be the same. –  RichardOD Apr 30 '09 at 14:34

That it only refers to LINQ to SQL

share|improve this answer
Totally agree. LINQ to XML and LINQ to objects are fantastic. –  Jeff Yates Apr 22 '09 at 14:34
LINQ to XSD is even better! –  Simon_Weaver Dec 22 '09 at 2:48

The biggest mistake people make when using LINQ is the same as when people try to use any sort of technology that lies on top of a technology that they don't have any good grounding in.

If you can't understand proper/efficient DB querying, you will screw up with LINQ.

If you can't understand the basic fundamentals of ADO.NET and data access, you'll probably screw up.

People think that by using LINQ it will allow them to coast by, but it won't.

share|improve this answer
So true, people who don't even get the idea of a transaction often make the most stupid mistakes with LINQ (i.e. reusing a query because they don't get the lazy nature of IQueryable, hogging the DB server, too...) –  emaster70 Apr 22 '09 at 17:01
@TheTXI Your answer demonstrates a classic LINQ mistake by assuming Linq = Linq TO SQL. Check out J. Skeets C# in Depth for more information. –  Ash Jan 21 '10 at 17:16

One basic one that I see in LINQ to SQL is not understanding DataContext. It is a Unit of Work object and should be re-created for each unit of work.

share|improve this answer
Examples/proof for the less educated among us? –  Adam Robinson Apr 22 '09 at 14:31

Somethings which come to mind are

  • It must be slower, better use plain C#
  • Trying to use it where simple C# would be more readable/manageable
share|improve this answer
Both of these points are excellent. I recently saw a dozen or so lines of code preceded by a comment that it was done this way to improve performance. I ran a benchmark on it to show the author that a one-line LINQ statement actually ran faster than the optimized loop he had. –  StriplingWarrior Jul 6 '10 at 19:28

Possibly, one of the misconceptions people might have is that the way a LINQ query is written, especially LINQ2SQL, has no impact on performance. One should always know what goes on in the background, if one intends to write code that has high performance, otherwise you might end up with interesting timeouts, OOMexceptions, stack overflow and such... =)

share|improve this answer
This is actually very much untrue. While LINQ2SQL likely won't suffer from this, LINQ to objects can be greatly affected by the ordering of query elements (for example, placing a where clause BEFORE a join can speed up the code considerably). –  Adam Robinson Apr 22 '09 at 14:13
I've traced linq2sql queries and have had greatly differing results on how and in what order I write conditions and predicates. –  J. Steen Apr 22 '09 at 14:15
Adam: That sounds like you're actually agreeing with the answer. It's a misconception that it has no impact on performance - in other words, it can have an effect on performance. –  Jon Skeet Apr 22 '09 at 14:15
@Jon: I think Adam is disagreeing with the "especially LINQ2SQL" –  Harper Shelby Apr 22 '09 at 14:17
D'oh. Looks like my reading comprehension needs some work. But thanks for the benefit of the doubt, Harper ;) –  Adam Robinson Apr 22 '09 at 14:20

Here is one, LINQ to SQL queries involving strings cause SQL Server procedure cache bloat People need to be aware of that

share|improve this answer

LINQ as a language is pretty straight forward and not so unexpected, especially if you're familiar with functional programming.

The concept of Deferred Execution is probably the biggest gotcha, and one of the best features. When you use LINQ that returns an IQueryable it's important to remember you are NOT executing whatever code you just wrote. It isn't until you call one of the methods that produces some other result that the query is executed.

Also, in terms of the LINQ to SQL provider, the biggest gotcha I've found is the performance cost. Turns out there is significant CPU cost to constructing SQL queries that are incurred every time the LINQ query is ran, unless you pre-compile your highly trafficked queries.

share|improve this answer

I totally agree with Adam Robinson, in fact the BIG mistake is that people stops on the beauty syntax not going deeper in the tech-facts, in terms of impacts or architectural views.

Sometimes people think about it as one thing when it's really another thing.. about that it's important to note Linq is a "Technology" and could be implemented in many ways, each of them could impact in different way about performance and design (for example), the basic syntax remain the same but the underlying things could changes.

Actually, starting from the great and growing implementations, there's not a complete list of best practices, the best practices could begin from:

  1. understanding before what kind of implementation will be used (Linq2Sql, Linq2Objects, Linq2CSV, Linq2Excel, Linq2LDAP, Linq2JSON, Linq2Xml, Linq2Xsd and more)
  2. then trying to understand what the basic technology features are intended in the choosed implementation
share|improve this answer

Speaking for myself, knowing when a sequence will be buffered or streamed is important.

Filling a buffer with large amounts of data will consume lots of memory. If possible, operations like reversing, counting, ordering, etc. should be done once the data has been reduced. In joins the left sequence is streamed and the right is buffered. When there's a significant difference in size, put the smaller one on the right.

share|improve this answer

A lot of people think that LINQ is 'Magical SQL' they can use in Code. It looks like SQL, but it's quite different. Understanding that it's difference and what it's really doing will prevent a lot of frustration.

share|improve this answer

Using linq2sql on tables with no primary keys (and not defining one in the designer).

Specially if what they are doing is an update, it doesn't update anything and you get no error.

share|improve this answer
Note that these items are specific to LINQ to SQL –  Richard Everett Apr 30 '09 at 8:40

I think understanding the point of the query execution is often a mistake (i.e. believing it's at the point of the query rather than at the point the data is first accessed), along with the belief that just because it compiles that it's going to run.

This in reference to Linq to SQL.

A fantastic tool for Linq is LinqPad by Joe Albahari, allowed me to learn Linq so much more quickly. If you don't have it, get it! And I'm not even on commission ;)

share|improve this answer

Understanding where you go from Linq2SQL/Entities to Linq2Objects. Understanding what is and is-not being executed in the DB engine versus in your application. Knowing when you are working with in-memory objects versus the underlying Linq provider that you are using, and then knowing how to intentionally transition from Linq2SQL to Linq2Objects whenever you need to perform post-query processing leveraging C#/.NET functions not supported in your provider.

share|improve this answer

Your Answer


