Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Many things in LINQ can be accomplished without the library. But for some scenarios, LINQ is most appropriate.

Examples are:

  1. SELECT - http://stackoverflow.com/questions/11883262/wrapping-list-items-inside-div-in-a-repeater
  2. SelectMany, Contains - http://stackoverflow.com/questions/11778979/better-code-pattern-for-checking-existence-of-value
  3. Enumerable.Range - http://stackoverflow.com/questions/11780128/scalable-c-sharp-code-for-creating-array-from-config-file
  4. WHERE http://stackoverflow.com/questions/13171850/trim-string-if-a-string-ends-with-a-specific-word

What factors to take into account when deciding between LINQ and regular .Net language elements?

share|improve this question
This makes for an interesting conversation, but unfortunately it's not a good fit for Programmers.SE because it solicits polling and extended discussion. –  Jim G. Aug 2 '12 at 15:43
What it seems to have actually solicited so far is two honest attempts at a thoughtful and comprehensive answer. No polling, and the only discussion so far is this tangent we're currently on. –  Sean U Aug 2 '12 at 16:19
@JarrodRoberson You can keep saying that it's polling for options. . . but with all visible evidence suggesting the people who are taking the time to answer the question are not interpreting it that way, it comes out looking like a thin excuse by people who didn't have any intention to spend much time on the question anyway. Which I don't think is very constructive. –  Sean U Aug 3 '12 at 13:39
@SeanU really, like the OP spent an extravagant amount of time on the question or research before asking it here? It is literally two links to other questions that people are expected to read and then post answers to those questions here, your argument doesn't make any sense. And all the answers so far are just lists of things in no order and just opinions at best. –  Jarrod Roberson Aug 3 '12 at 21:27
Of course there is no correct answer, but it's still a legitimate question! –  AndreasScheinert Nov 2 '12 at 8:35
show 3 more comments

2 Answers

up vote 6 down vote accepted

I've found LINQ to be handy in literally any scenario where I'm working with collections of objects. The main things you do with collections of objects are:

  • Search them for items matching some characteristic
  • Traverse them to calculate some aggregate value
  • Visit each item in the collection and calculate some value based on it (translation)
  • Rearranging its contents for some purpose (transformation)

Each of these scenarios has a number of LINQ methods that are designed to help with the task:

  • For searching, there's Where(), First(), Any(), All(), Last()
  • For aggregation, there's Aggregate(), Average() Sum(), Max(), Min()
  • For translation there's Select() and SelectMany()
  • For transformation there's OrderBy(), GroupBy(), Reverse()

And a few other odds and ends that I'm not sure how to categorize in the above for - for example the set operations Union() and Intersect().

In general, all of these functions do the work of hand-coded loops, but have the advantage of improving legibility because they help to replace several lines worth of code with a few LINQ operations that are named after what they do. For example, consider

string[] input;

var output = new string[input.Length];
for(int i = 0; i < input.Length; i++)
    output[input.Length - i - 1] = input[i];


var output = input.Reverse().ToArray();

Another advantage is that it's easy to string them together to get more complex behavior. Say that instead you wanted to get only the unique values in the array, and sort them. Right now I'm feeling too lazy to hand-code a solution to it, but I'm sure whatever it is it would be much longer and harder to read than

var output = input.Distinct().OrderBy(x=>x).ToArray();

And it definitely would take longer, which is why I don't feel like writing it.

share|improve this answer
add comment

There are a number of different types of technologies which fall under the umbrella term of LINQ:






And there are more provided by others as well, such as LINQ-to-CSV, LINQ-to-NHibernate, etc.

In every one of these cases, the purpose of LINQ is the same: to query sets of data. So anytime you have some sort of set (0, 1 or more items) of a particular type of data, LINQ is a succinct solution to deal with it. Many questions seem to arise over whether it performs better than a hand-written approach, but that's not the point. LINQ specifies intent rather than implementation.

share|improve this answer
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.