I would like to know if Deferred Execution can be used for queries in method syntax and query syntax , I'm interested to know if there are any difference. Thanks
3 Answers
Actually there is only method syntax. When you write your query with query syntax, compiler translates it to method syntax (actually to static methods calls).
Example:
Extension method call (method syntax)
var query = sequence.Select(x => x.Property);
Is compiled as (yes extension methods are just a syntax sugar for calls of static class methods)
var query = Queryable.Select(sequence, x => x.Property);
Same result gives (this is a syntax sugar for same Queryable/Enumerable methods calls)
var query = from x in sequence
select x.Property;
So, both syntaxes produce same code. Thus there is no difference which syntax you are using - deferred execution (and anything else) will work in the same way.
1 Comment
Query syntax is just syntactic sugar for method syntax, so there is no difference.
2 Comments
Deferred execution is same for both. Query syntax actually compiles to method syntax at compile time.
LINQ Query Syntax versus Method Syntax (C#) - MSDN
Most queries in the introductory LINQ documentation are written as query expressions by using the declarative query syntax introduced in C# 3.0. However, the .NET common language runtime (CLR) has no notion of query syntax in itself. Therefore, at compile time, query expressions are translated to something that the CLR does understand: method calls. These methods are called the standard query operators, and they have names such as Where, Select, GroupBy, Join, Max, Average, and so on. You can call them directly by using method syntax instead of query syntax.