30
  • What are the pros and cons of LINQ (Language-Integrated Query)?
  • What are the best and worst cases in which to use LINQ?
  • How have you benefitted or not benefitted from using LINQ?
  • Which data sources benefit the least and the most from LINQ?
George Stocker
  • 57,289
  • 29
  • 176
  • 237
Middletone
  • 4,190
  • 12
  • 53
  • 74

5 Answers5

35

I'm a massive fan of LINQ - although it needs to be kept in perspective, and not treated as a silver bullet.

Pros:

  • Declarative approach makes queries easier to understand and more compact
  • Extensibility and expression trees allow mostly consistent querying of multiple sources
  • Even in-process queries can be implemented in ways other than LINQ to Objects - e.g. Parallel LINQ and my own Push LINQ framework. Very flexible.
  • Fabulously useful for in-process queries, where it's easiest to understand
  • Great to be able to avoid SQL in strings etc
  • Wide range of operators provided by default, and others can easily be added for LINQ to Objects
  • Language features introduced primarily for LINQ are widely applicable elsewhere (yay for lambdas)

Cons:

  • Query expressions aren't understood well enough, and are overused. Often simple method invocation is shorter and simpler.
  • Inevitable inconsistencies between provider - impedance mismatch is still present, which is reasonable but needs to be understood
  • There will always be some things you can do in SQL but not in LINQ
  • Without understanding what's going on, it's easy to write very inefficient code
  • It's hard to write a LINQ provider. This may well be inevitable, but more guidance from Microsoft would be appreciated.
  • It's a new way of thinking about data access for most developers, and will need time for understanding to percolate
  • Not specifically LINQ but related to it - the way extension methods are discovered in C# isn't granular enough
  • Some operators are "missing", particularly the equivalents of OrderBy for things other than ordering - e.g. finding the item with the maximum value of a property
  • Deferred execution and streaming are poorly understood (but improving)
  • Debugging can be very tricky due to deferred execution and streaming
  • In some specific cases, LINQ can be significantly slower than manual code. The better you understand the internal workings, the better you'll be able to predict this. (And of course, if performance is important to you, you should have appropriate tests around it.)

I find it's best when dealing with in-process queries. They're easy to predict, understand and extend. Complementary technologies like LINQ to XML and Parallel LINQ are great. LINQ to Objects can be used almost anywhere.

LINQ to SQL etc are really nice where they're appropriate, but they're harder to understand and need more expertise. They're also only applicable in certain areas of your code.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • dam thats a lot of cons but I still love it – Nathan W Nov 07 '08 at 07:09
  • 15
    They should rename the site Skeetoverflow IMO. – cfeduke Nov 07 '08 at 07:20
  • 3
    @Nathan: Yes, the list of cons is daunting - but the pros are much more important :) I miss LINQ all the time when I'm writing in Java :( – Jon Skeet Nov 07 '08 at 07:30
  • 2
    Isn't a little less than half of the con list the fault of developers and not LINQ itself? – John Farrell Dec 17 '08 at 14:52
  • 4
    @jfar: Potentially, but they're still "cons". – Jon Skeet Dec 17 '08 at 15:13
  • 1
    @jfar: as easy-to-read and easy-to-use are pros of languages, easy-to-misunderstand and easy-to-create-error should be cons :) – Delta76 Aug 04 '10 at 06:25
  • I would suggest a Con: Speed. Old style loops will routinely be considerably faster, specially for larger data sets. I am ambivalent to Linq, it is horses-for-courses. – Steve Hibbert Apr 25 '17 at 10:00
  • @SteveHibbert: That kind of *blanket* statement about performance is just begging for more detail. In very, very many cases the difference will be absolutely negligible, regardless of data set size. (It's not even clear from your comment whether you're talking about LINQ to Objects or out-of-process LINQ, for a start...) – Jon Skeet Apr 25 '17 at 10:02
  • Apologies, did not mean to blanket denigrate LINQ, I use Linq to XML a lot and Linq with collections, Count(), First() etc, all good. My experience is mostly LinqToObjects. Example, Except() on dictionaries with 1milion entries was six times slower than iteration [http://stackoverflow.com/questions/8851155/finding-difference-between-two-dictionaries/43477122#43477122]. This may not matter in 99% of cases, but people should be aware of a potential performance cost with Linq to Objects. – Steve Hibbert Apr 25 '17 at 13:26
  • @SteveHibbert: Yes, there will certainly be *special* cases where that happens. But I'd say it's the exception rather than the norm. I'll add something suitable caveated in the cons. – Jon Skeet Apr 25 '17 at 13:40
3

My favorite part: using them to simplify writing unit tests. Also IEnumerable chains have urged me to write more fluent interfaces in my code.

Cons: Lambdas and extension methods are my hammers and all problems are nails.

Overall: breathed new life into programming in C# for me.

cfeduke
  • 23,100
  • 10
  • 61
  • 65
3

There is a problem with them of sneaking exceptions out of try catch blocks by way of delayed execution.

for example:

var l = new List<int>() {1, 2, 3};
try
{
    l.Select(x => x / 0);
}
catch
{
    // error
}

l.elementAt(0); // exception occurs here outside of the try catch

Which can be tricky the first time you run into it, especially as the debugger will point you at the code inside the try-catch.

Otherwise I find them incredibly useful and very time saving.

Toby
  • 2,333
  • 3
  • 16
  • 10
  • 1
    The example as written will work just fine. As you said, LINQ can be sneaky but not in the sense you implied :) The call to Select will do nothing - it will return IEnumerable which needs to be iterated to get some result. Thus no exception will be raised, it's normal and by design, plus the collection won't be modified in any way. Therefore the last call will succeed returning 1. – rawpower Nov 29 '10 at 15:45
2

Pro:

Con:

  • Like any new technology too many people don't understand it but still use it

@Jon Skeet - another great response, you steal everyones thunder :P. I totally agree about how hard writing a provider is, I'm in the process of it at the moment! Are you familiar with Bart De Smet? He's got lot of good examples of doing so.

Aaron Powell
  • 24,927
  • 18
  • 98
  • 150
1

I've used LINQ mainly to work on collection of objects. LINQ works wonderfully with object collections, removing the need of predicate functions in most cases.

I tried using LINQ to SQL a little while ago, but found it underpowered and clumsy. In particular I couldn't bring myself to use the SQL Database class designer. Maybe it does give intellisense on the database, but who needs it when you've got SQL?

Let me tell you though, it's certainly a good idea to learn more about LINQ, as the applications in the future should only increase.

Cyril Gupta
  • 13,505
  • 11
  • 64
  • 87