Update: You fixed your title, so ignore the bombast.
The name of your question has nothing to do with your code samples. Your question implies that one syntax is IEnumerable and the other is IQueryable, but this is not true. In your samples, if db.Surveys is IQueryable, then both samples use IQueryable. I will try to answer both questions.
Your two code examples are just different ways of writing the same LINQ queries (assuming they are well written). The code in example 1 is simply an abbreviation of the code in example 2. The compiler processes the code in both samples in the same way. Think about how the C # compiler will handle int? same as Nullable<System.Int32> . Both C # and VB.Net provide this short query syntax. Other languages ββmay not have this syntax, and you will have to use the syntax of sample 2. In fact, other languages ββmay not even support extension methods or lambda expressions, and you will have to use an even uglier syntax.
Update:
To take Sander's example further when you write this (query understanding syntax):
var surveyNames = from s in db.Surveys select s.Name
You think the compiler turns this shorthand into this (extension methods and lambda expressions):
IQueryable<string> surveryNames = db.Surveys.Select(s => s.Name);
But in fact, extension methods and lambda expressions are shorthand. Compilers emit something like this (not really, but just to give an idea):
Expression<Func<Survey, string>> selector = delegate(Survey s) { return s.Name; }; IQueryable<string> surveryNames = Queryable.Select(db.Surveys, selector);
Note that Select() is only a static method in the Queryable class. If your .NET language does not support query syntax, lambdas or extension methods, this is somehow the way you have to write code yourself.
What are the benefits of using one style over another?
For small queries, extension methods may be more compact:
var items = source.Where(s => s > 5);
In addition, the syntax of the extension method can be more flexible, for example conditional, if the sentences:
var items = source.Where(s => s > 5); if(smallerThanThen) items = items.Where(s => s < 10); if(even) items = items.Where(s => (s % 2) == 0); return items.OrderBy(s => s);
In addition, several methods are available only through the syntax of the extension method (Count (), Aggregate (), Take (), Skip (), ToList (), ToArray (), etc.), so if I use one of Of these, I usually write the entire query in this syntax so as not to mix both syntaxes.
var floridaCount = source.Count(s => s.State == "FL"); var items = source .Where(s => s > 5) .Skip(5) .Take(3) .ToList();
On the other hand, when a query becomes larger and more complex, the syntax for understanding the query may be clearer, especially after you begin to complicate a few let , group , join , etc.
In the end, I usually use what works best for each particular request.
Update: you fixed your title, so ignore the rest ...
Now about your title: regarding LINQ, IEnumerable and IQueryable are very similar. Both of them have basically the same extension methods (Select, Where, Count, etc.), with the main difference (only?) Is that IEnumerable accepts Func<TIn,TOut> as parameters, and IQueryable accepts Expression<Func<TIn,TOut>> as parameters. You express the same thing (usually lamba expressions), but internally they are completely different.
IEnumerable is the door to LINQ to Objects. LINQ to Objects extension methods can be called in any IEnumerable (arrays, lists, anything you can repeat with foreach ), and Func<TIn,TOut> converted to IL at compile time and works like regular method code at runtime. Note that some other LINQ providers use IEnumerable and therefore actually use LINQ for objects behind the scenes (LINQ to XML, LINQ to DataSet).
IQueryable is used by LINQ to SQL, LINQ to Entities, and other LINQ providers who should check your query and translate it instead of directly executing the code. IQueryable queries and their Expression<Func<TIn,TOut>> do not compile to IL at compile time. Instead, an expression tree is created and can be checked at runtime. This allows you to convert statements to other query languages ββ(for example, T-SQL). The expression tree can be compiled into Func <TIn, TOut> at run time and executed if desired.
An example illustrating the difference can be found in this question , where the OP wants to execute part of a LINQ to SQL query in SQL Server, cast objects to and execute the rest of the query in LINQ to Objects. To do this, all he needs to do is enable IQueryable in IEnumerable, where he wants the switch to happen.