LINQ Lambda | Query Syntax VS Method Syntax Performance [duplicate]

6

Doubt regarding construction and performance of querys using Query Syntax and Method Syntax / LINQ

Query Syntax:

var VendorQuery = from v in vendors
                  where v.CompanyName.Contains("Toy")
                  orderby v.CompanyName
                  select v;

Method Syntax

var vendorQuery = vendors
                  .Where(v => v.CompanyName.Contains("Toy"))
                  .OrderBy(v => v.CompanyName);
The querys above are fictitious, just to illustrate.

  
  • In both situations the same query is mounted?
  •   
  • Assuming I have to do a relatively "heavy" search, it does   difference using one or the other?
  •   
        
    asked by anonymous 27.12.2018 / 10:58

    1 answer

    5

    Yes it's the same performance for your example. And according to the documentation:

      

    Most queries in the LINQ introductory documentation (Query   Integrated Language) is written using the query syntax   of LINQ. However, the query syntax must be   converted into method calls to the Common Language Runtime (CLR)   when the code is compiled.

    Taking into consideration the specified above in the same one you will use as long as the logic is the same.

    To know about performance you have to check the query generated. One tip for you to observe the query generated is to do the following in your Context :

    public DatabaseContext() : base("MeuContext")
    {
        //Trecho abaixo utilizado para ver as querys geradas pelo Entity
        #if DEBUG
        Database.Log = s => System.Diagnostics.Debug.WriteLine(s);
        #endif
    }
    

    This System.Diagnostics.Debug.WriteLine(s) excerpt will cause the query generated to appear in the Output when you are debugging and will help you better understand the querys generation. You can even test the example you gave here in the question to see if it actually generates the same query.

    Reference:

    link

        
    27.12.2018 / 11:22