3

This is sort of a follow up on this question I read: What is the biggest mistake people make when starting to use LINQ?

The top answer is "That it should be used for everything." That made me wonder what exactly that means.

What are some common examples where someone used LINQ when they should not have?

Community
  • 1
  • 1
dtc
  • 10,136
  • 16
  • 78
  • 104

4 Answers4

4

You shouldn't use LINQ when the alternative is simpler or significantly more efficient.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • 2
    an example? LINQ is quite specific so you can't, and wouldn't, use it for anything and everything. I find it generally makes things simpler. As for efficiency then yes, its easy to perform more efficiently without LINQ. And you're right, only when it is SIGNIFICANT...good point – andy Apr 23 '09 at 00:59
3

I would suggest avoiding LINQ anytime it made the code less obvious.

However, in general, I think LINQ makes things easier to follow, not more difficult, so I rarely avoid it.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
1

It is possible for LINQ to be significantly slower than the alternatives, especially if you have a lot of intermediate Lists. However you are talking about some pretty large datasets, so large that I haven't encountered them.

However, one thing to keep in mind is that a well-written LINQ query can also be much faster than the alternative because of the way IEnumerable works.

Finally, using LINQ now will allow you to switch to Parallel LINQ when it is released with little or no changes.

Jonathan Allen
  • 68,373
  • 70
  • 259
  • 447
0

It's still okay to use foreach. :)

JP Alioto
  • 44,864
  • 6
  • 88
  • 112