Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. What are the differences ?

What are the differences ?

Scheduled Pinned Locked Moved LINQ
question
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mohammad Dayyan
    wrote on last edited by
    #1

    Hi. Could you tell me what the differences between query1 and query2

    Northwnd nw = new Northwnd(@"northwnd.mdf");

    var query1 =
    from cust in nw.Customers
    where cust.City == "London"
    select cust;

    var query2 = nw.Customers.where(q => (q.City == "London"));

    Thank you.

    F J 2 Replies Last reply
    0
    • M Mohammad Dayyan

      Hi. Could you tell me what the differences between query1 and query2

      Northwnd nw = new Northwnd(@"northwnd.mdf");

      var query1 =
      from cust in nw.Customers
      where cust.City == "London"
      select cust;

      var query2 = nw.Customers.where(q => (q.City == "London"));

      Thank you.

      F Offline
      F Offline
      Fernando Soto
      wrote on last edited by
      #2

      Hi Mohammad; Here are some differences: query1: 1: This form of the query is called Query Comprehension Syntax. The query keywords such as from, where, orderby, ...select and others are part of the C# language. 2. This form always starts with a from clause and ends with either a select or group clause. 3. Its syntax flows much like a SQL statement but is NOT SQL 4. At compile time the compiler transform the Query Comprehension Syntax to Lambda Syntax. 5. In most but not all cases this form is easier to understand and use. query2: 1. This form of the query is called Lambda Syntax. 2. The operators in this syntax such as where, order by, select and others are implemented as extension methods. 3. Lambda expressions are used to process the sequences, in your sample this is the Lambda expression q => (q.City == "London" 4. There are many operators that do not have Query Comprehension Syntax equivalent and for those you need Lambda syntax. Fernando :)

      M 1 Reply Last reply
      0
      • M Mohammad Dayyan

        Hi. Could you tell me what the differences between query1 and query2

        Northwnd nw = new Northwnd(@"northwnd.mdf");

        var query1 =
        from cust in nw.Customers
        where cust.City == "London"
        select cust;

        var query2 = nw.Customers.where(q => (q.City == "London"));

        Thank you.

        J Offline
        J Offline
        Jon Rista
        wrote on last edited by
        #3

        Technically speaking, there is no functional difference. In actuality, the C# compiler will transform query1 into something very much like query2, if not even exactly like it. The only real difference is syntactic. For simple queries, its probably best to use notation like the latter. However, for complex queries, using the extension methods and lambda expressions can result in very long, deeply nested, difficult to maintain and understand queries. In these cases, its better to use notation like query1.

        1 Reply Last reply
        0
        • F Fernando Soto

          Hi Mohammad; Here are some differences: query1: 1: This form of the query is called Query Comprehension Syntax. The query keywords such as from, where, orderby, ...select and others are part of the C# language. 2. This form always starts with a from clause and ends with either a select or group clause. 3. Its syntax flows much like a SQL statement but is NOT SQL 4. At compile time the compiler transform the Query Comprehension Syntax to Lambda Syntax. 5. In most but not all cases this form is easier to understand and use. query2: 1. This form of the query is called Lambda Syntax. 2. The operators in this syntax such as where, order by, select and others are implemented as extension methods. 3. Lambda expressions are used to process the sequences, in your sample this is the Lambda expression q => (q.City == "London" 4. There are many operators that do not have Query Comprehension Syntax equivalent and for those you need Lambda syntax. Fernando :)

          M Offline
          M Offline
          Mohammad Dayyan
          wrote on last edited by
          #4

          Nice, tanks

          F 1 Reply Last reply
          0
          • M Mohammad Dayyan

            Nice, tanks

            F Offline
            F Offline
            Fernando Soto
            wrote on last edited by
            #5

            Not a problem, glad I was able to help :)

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups