views:

464

answers:

8

This is a question about LINQ the .NET language feature, not LINQ to SQL.

I find that the ability to query almost any in memory collection in such a consistent and declarative way is one of the best language features I've ever come across.

I find it extremely useful, and I love the clean abstraction it provides. LINQ can even be used to query Strings, indeed its extremely flexible.

My questions are:

  1. Is this idea of "Language Integrated Queries" new? and if not, what other languages implement a similar feature
  2. How do other implementations compare to Microsoft's?
  3. This is a little subjective, but how important do others feel LINQ, as an idea, is?

cheers

+3  A: 

Speaking as a querying of in memory collections, no this is not a unique feature. Take for instance F#'s pipe forward operator. It allows you to write very similar style operations as you would a linq query.

Take for instance the following code which extracts digits from a string and sums them

"foobar42".ToCharArray() 
  |> Seq.filter System.Char.IsDigit 
  |> Seq.map (fun x -> x.ToString() ) 
  |> Seq.map System.Int32.Parse 
  |> Seq.sum
JaredPar
hey Jared, do you feel that F#'s implementation provides the intuitive and clean abstraction that LINQ does? I think LINQ's power is both the implementation and syntax no?
andy
@andy I think the intuitiveness of F# really depends on your background. The first few times I picked up F# I found the syntax extremely unintuitive and hard to follow but I don't have a strong functional background. Friends of mine who do have strong functional backgrounds found it rather easy to follow. Over time I've gotten used to it and now find it very easy to follow and intuitive.
JaredPar
@Jared: excellent point. I guess I'm pointing to the way MS created a syntax that is loosely based on SQL. Indeed SQL is kind of the ultimate querying language, that's it sole purpose. To create syntax which models itself on SQL makes it seem intuitive...?
andy
+6  A: 

The idea of language integerated query is not new, the way MS puts it is unique and new. It is affected by Haskell and SQL syntax, Haskell had the idea of enumerators and lazy evaluation which is the core underline principle in implementing LINQ, in fact LINQ is just a syntactic sugar.

I feel link is a great bridge between reasoning about your programs and data in an abstract and clean way.

mfawzymkh
yeah, actually I'd say the syntactic sugar is really at the core of LINQ's power
andy
maybe syntactic crystal meth?
geofftnz
+2  A: 

I think LINQ is very interesting.

Remember that it essentially adds powerful functional programming capabilities into .NET

It is also interesting that Microsoft did not have to change the runtime to make LINQ work, all the magic is because of new language features in C# 3 and in the compiler and in class library - because essentially your queries gets internally translated to lambda syntax (see LINQPad for good demonstrations of this)

I believe one of the reasons MS developed this kind of technology is to make it easier to parallelize programs - SQL might become less relevant because of LINQ.

dplante
@dplante: don't leave out the last step that makes LINQ work. Expression trees. Maybe other modern compilers can parse an expression and save the expression tree in a runtime-accessible structure, but LINQ does. It allows the LINQ provider to interpret the expression tree to produce whatever the underlying data store requires in order to meet the developer's intent.
John Saunders
+2  A: 

I'm thinking LINQ.NET started it all. Nowadays, there are several other implementations: http://en.wikipedia.org/wiki/Linq#Other_language_implementations, but MS LINQ was the first if I remember correctly.

WebDevHobo
+1  A: 

Within Microsoft, a very similar syntax was developed in C-omega (Cw) before it was adopted by C# and VB.

Michael Dunn
+1  A: 

The concepts in Linq (Where, Select, Aggregate) have been in LISP for years now, these aren't new at all. The implementation in C# is particularly elegant though, especially how it manages to add a pretty disruptive concept yet blend so well with existing C# 2.0 code. The "reinterpret Linq AST as SQL" is also pretty damn clever.

Paul Betts
hmm, thanks Paul. Yeah, I'd really say its the elegance of MS's implementation which makes it so useful..?
andy
+2  A: 

Linq has a lot to do with List comprehensions you can find in functional languages and in more mainstream languages like Python, where it's been a standard and very widely used feature for quite a while now.

For a good exemple of list comprehensions, this Python function taken from the simple spell checker by Peter Norvig

def edits1(word):
   s = [(word[:i], word[i:]) for i in range(len(word) + 1)]
   deletes    = [a + b[1:] for a, b in s if b]
   transposes = [a + b[1] + b[0] + b[2:] for a, b in s if len(b)>1]
   replaces   = [a + c + b[1:] for a, b in s for c in alphabet if b]
   inserts    = [a + c + b     for a, b in s for c in alphabet]
   return set(deletes + transposes + replaces + inserts)

It looks a lot like Linq, with the (implicit) select written at the start of the expression, instead of being weirdly at the end like in Linq.

lambdas are somewhat frown upon in Python, and the default list comprehensions in Python are eager (think List<T>) instead of lazy (IEnumerable<T>) but it's very close.

As for the concept of having (somewhat) pure function chaining, like other people have replied here, it's one of the oldest concept in computer languages (Lisp being over 50 now...)

Yann Schwartz
Wow, that code for the spell checker is really, really nice!
Casebash
+2  A: 

Is this idea of "Language Integrated Queries" new? and if not, what other languages implement a similar feature

If you consider sql a language, then no. I think MS's insight was that everything in an array like container is essentially an in memory database. Not only that, but many of the operations over these collections are the same. Programmers must write millions of .selects, .wheres, and .firsts over the course of a year. Many of these implementations have temporary arrays and variable that clutter code. With LINQ, the logic is made far more concise, without the temp/holder variable/arrays. MS wiped out an entire class of bugs with the standard LINQ operators.

How do other implementations compare to Microsoft's? Not sure

This is a little subjective, but how important do others feel LINQ, as an idea, is?

I feel that LINQ is quite possibly the most revolutionary language feature I've ever seen, and I had thought closures were pretty awesome also. It makes programs cleaner, and programmers far more productive.

Steve
thanks Steve, excellent articulation of what LINQ has achieved and made possible
andy