1

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 3

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Well, actually I pointed that both syntaxes produce same result (static method calls). If result is same, then deferred execution works in the same way :) But agree, I'll add this remark
3

Query syntax is just syntactic sugar for method syntax, so there is no difference.

2 Comments

But does not mention deferred execution which seem to be the core topic.
The is no difference in anything, including deferred execution, because both method and query syntax are the same under the hood.
3

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.

1 Comment

+ the only one you has at least mentioned the core topic "deferred execution".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.