tags:

views:

96

answers:

3

In what scenarios is LINQ best applicable?

Would it be good "sense" to suggest to use LINQ to query all kinds of collections?

+7  A: 

This is a very broad question so I have to provide a very broad answer

LINQ is best applicable in any scenario where you have data represented in an IEnumerable and you must inspect, query, aggregate or project this data in some manner.

JaredPar
Or IQueryable ... which is also IEnumerable ... so yeah.
Richard Hein
+1  A: 

Linq is best applicable when you want to query against data.

It runs the gambit from Linq to SQL to Ling to Amazon.

ShaneC
+1  A: 

LINQ provides a common syntax for querying datasources - SQL, XML, Objects etc. The underlying provider is responsible for converting your LINQ query to a form appropriate for the datasource, which means all you have to do is 'plugin' the right provider through an interface.You now have nicely separated and consistent code for querying a range of different datasources.

flesh
This is important to realize, and to explain the benefits of LINQ as more than just "SQL" for C#. The idea of abstracting query interfaces is very important. There are countless ways to query data, but as flesh said, if a provider is available (or you make one), you can use the common interface and APIs. This reduces the amount of query languages and types and formats of expressions you need to learn and use in your work.
Richard Hein
Agreed, which is why I dont think Jared's answer is particularly good. As you say, it's not just 'SQL' but nor is it just for IEnumerable, LINQ works against any datasource for which a provider exists.
flesh
True, but all providers must implement IQueryProvider, which must return a reference to IQueryable, which in turn implements IEnumerable.
Richard Hein