tags:

views:

281

answers:

8

For example, why do you do this in LINQ

var products = from p in Products
               select p.Name;

when they could have done this:

var products = select p.Name from Products p;

Does the second offer some limitations in linq? Maybe the above examples are too simple to actually see why linq is written in one order and sql is written in another.

+17  A: 

Because in order to have Intellisense in Visual Studio working with LINQ, it needs to know the tables first so that the editor can offer the programmer the list of columns to choose from. If you do it the SQL way and first you choose the columns, the editor can't really help you as it doesn't know which tables to look at.

DrJokepu
I remember an old Channel 9 interview with Anders in which he mentions this. And LINQ is also influenced by other query languages like XQuery, not just SQL.
Mehrdad Afshari
Actually - that's pretty obvious.
Arnis L.
The only reason can't be intellisense, right?
Xaisoft
@Xaisoft even if it's not - reason is good enough.
Arnis L.
+19  A: 

Because LINQ is not SQL. LINQ consists of a number of chained extension methods on IEnumerable<T> (when you are using the System.Linq namespace). The SQL like syntax is just a compiler trick to enable some syntactic sugar on chains of such query methods so it appears you can use queries looking a bit like SQL inside .NET languages. LINQ basically has nothing to do with SQL per se...

peSHIr
+1: good answer
Arthur
ok, I see what you are saying. It makes more sense now. It seemed as if when they talked about LINQ, they were trying to replace writing writing sql queries on the database side with using linq queries on the code side.
Xaisoft
It's better to think of LINQ as an effort to support *queries* in code, not SQL in code. The vast majority of SQL isn't supported, nor should it be.
dahlbyk
http://www.youtube.com/watch?v=8Mttjyf-8P4 just for fun, one of the creator of linq about the inner philosophy of linq. ( by the way, he develop an interessting point of view about dependancy injection and mocking )
Antoine Claval
Lots of people who first saw LINQ thought "Cool! SQL in my code" and never actually got what LINQ was. Basically LINQ is the beginning of the current functional programming paradigm shift in .NET. In fact it's rather clever to get "us" to start using this because of querying, thinking of SQL, getting programmers ready for functional programming without them even noticing. ;-)
peSHIr
Also, it's important to realize that LINQ does not require extension methods, nor is it constrained to IEnumerable<T>. Fundamentally, query expressions are just syntactic sugar that the compiler translates into method calls that are resolved in the normal way. The most common source of query operators are the extension methods in System.Linq, but they are not the only ones.
dahlbyk
Linq has nothing to do with IEnumerable<T> or extension methods specifically, actually all it requires is that some methods named Select, Where, etc. are callable on the objects you work with. Linq is monadic translation of query like expressions to method calls, that's it.
Pop Catalin
+4  A: 

It is often cited that the reason is Intellisense. Because if you start writing

select p.

the compiler can't say what properties to show you.

but if you start with

from person p select p.

it knows to look up properties on the person object.

HTH alex

AlexDuggleby
+4  A: 

Intellisense is often cited as a reason, but I think a better explanation is that Select logically happens at the end of the query writing process. First you set up your data sources, then you filter and group, then once you have everything in place you specify what you want to get out.

dahlbyk
+1  A: 

While others have correctly stated that providing intellisense was part of the reason that influenced Ander's decision, something else that needs to be considered is that LINQ is not just intended to be used with SQL. As it's a much more encompassing technology, LINQ was never intended to be constrained to just work the way that SQL does.

Pete OHanlon
+3  A: 

Extending what peSHIr said.

LINQ is based on the mathematical principles of lambda calculus.

You need to study/understand how LINQ query operators map to actual methods.

leppie
thanks for the link on linq, lol
Xaisoft
+10  A: 

Leaving aside IntelliSense, query expression expansion etc (which are all valid reason), I actually believe the LINQ way makes a lot more sense.

You start with a data source. You apply filters, ordering etc. You finish with a projection. In other words, the query is written in the logical order of operations.

To put it a different way: why on earth did the SQL designers choose to order SQL queries that way?

Jon Skeet
Indeed! That's the real question here...
peSHIr
@Jon: Because all programmers think SQL is procedural!
leppie
+2  A: 

Actually, LINQ's syntax is more closely based on XQuery than it is on SQL, and XQuery does it that way, too.

The main reasons have already been given: in C#, VB.NET and indeed most programming languages, scope flows from top to bottom and left to right, just the way we normally read. The way SQL is written, scope jumps around: an identifier gets already used in the SELECT part of the query, but it only gets introduced later, in the FROM part of the query. That's why the designers of XQuery decided to flip it around and LINQ just follows.

It also matches the mental model better: you have list of data sources (FROM), then you filter out the data you're interested in (WHERE), then you sort this data (ORDERBY) and lastly you either project it into a different representation (SELECT) or partition the results (GROUP BY). After that, you can inject the results into the next query (INTO).

Jörg W Mittag