tags:

views:

1054

answers:

14

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?

+32  A: 

That it should be used for everything.

Adam Robinson
I'd up-vote this twice if I could!
JTA
Yeah, I definitely went through this phase. I like to think that I've managed to defeat the tendency now, though. :) Good thing for my employer that all my Linq abuse took place on my own time with personal projects!
Greg D
It's certainly important to recognise when it isn't the right approach.
Jeff Yates
I see lots of programmers who overuse implicitly typed local variables.
RichardOD
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
+3  A: 

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... =)

J. Steen
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
I've traced linq2sql queries and have had greatly differing results on how and in what order I write conditions and predicates.
J. Steen
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
@Jon: I think Adam is disagreeing with the "especially LINQ2SQL"
Harper Shelby
@metaphor, what you wrote in your answer doesn't jive with your comment.
Chris Lively
D'oh. Looks like my reading comprehension needs some work. But thanks for the benefit of the doubt, Harper ;)
Adam Robinson
@Christ, it doesn't? I thought I said that LINQ2SQL suffers, and that my comment says I've determined this empirically. Hmm. "differing results" might make one thing I meant the actual query result. Dang.
J. Steen
@Chris! Not Christ. Yes.
J. Steen
+3  A: 

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

SQLMenace
+11  A: 

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.

TheTXI
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
@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
+16  A: 

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

.First()
.FirstOrDefault()
.Single()
.SingleOrDefault()

Not understanding deferred execution.

Richard Ev
+1 for noting Single. I've been using First and commenting that "// There can be only one".
Greg D
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
A: 

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 ;)

Lazarus
+9  A: 

That it only refers to LINQ to SQL

Scott Weinstein
Totally agree. LINQ to XML and LINQ to objects are fantastic.
Jeff Yates
LINQ to XSD is even better!
Simon_Weaver
+1  A: 

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.

JD Conley
+3  A: 

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
Niran
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
+4  A: 

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.

Even Mien
Examples/proof for the less educated among us?
Adam Robinson
http://iridescence.no/post/ASPNET-MVC-DataContext-and-The-Unit-of-Work-Pattern.aspx
Even Mien
+1  A: 

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
Hoghweed
+1  A: 

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.

eglasius
Note that these items are specific to LINQ to SQL
Richard Ev
+1  A: 

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.

Rob P.
+1  A: 

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.

Matthew Sposato