locked
what is the order of exeution in linq. RRS feed

  • Question

  • User2102072086 posted

    like in sqlserver we have following order of execution.

    SELECT ORDER

    FROM

    1. ON

    2. JOIN

    3. WHERE

    4. GROUP BY

    5. WITH CUBE or WITH ROLLUP

    6. HAVING

    7. SELECT

    8. DISTINCT

    9. ORDER BY

    10. TOP

    can i get the same msdn page where they have written the above thing for both sntaxs of linq.

    yours sinerley

    Thursday, April 9, 2020 6:16 AM

All replies

  • User753101303 posted

    Hi,

    Linq creates a SQL statement for you which then run on the db side so you likely won't find a separate documentation on that.

    Still you can use Linq to create a wrong SQL statement that won't produce the result you expect (I remember to have seen someone doing that maybe with a contradictory distinct/ordering). Depending on which version you are using you can use https://cmatskas.com/logging-and-tracing-with-entity-framework-6/ to trace generated SQL statements.

    If you need further help, posting your Linq query and explaining how it differs from the expected result will be likely easier to deal with...

    Thursday, April 9, 2020 9:37 AM
  • User-1330468790 posted

    Hi rajemessage,

     

    There is not a separate document like what you listed. 

    However, there are some information extracted from the LINQ Document which might help you to understand LINQ better.

    • Query syntax will be translated to method calls

    If you would like to know the order of the execution in linq, the first thing you should bear in mind is that LINQ can be written by using declarative query syntax, but the query syntax must be translated into method calls for the .NET common language runtime (CLR) when the code is compiled.

    Therefore, asking the question about the execution order in LINQ is the same as asking the method execution order. These methods, like Where, Select, are extension method

    • These Methods are extension methods

    You could find out all of the extension methods by clicking here

    • A query expression must begin with a from clause and must end with a select or group clause.

    As you may already know why the select or group clauses are put at the end of the query syntax, it will return the final result as you execute other methods.

      

    More information, you could spend time reading this document if you want to fully control the LINQ.

     

    Hope this can help you.

    Best regards,

    Sean

    Thursday, April 9, 2020 10:33 AM
  • User-857013053 posted

    For EF order prior to the select does not matter. The LINQ query is converted to a SQL query and run and the SQL query. Specifically with Include when the following statements modify the structure of the query, but a where clause doesn't do that.

    In other LINQ queries, LINQ-to-Objects, order can matter greatly as the query is not re-optimized the way SQL is and is simply processed from top to bottom, and some LINQ methods require previous methods to run to completion and process results before proceeding to even the first element (OrderBy for example).

    Thursday, May 14, 2020 5:11 AM