ITORIAN    

  public string Welcome() {

      return "Abhimanyu's Thoughts";

  }

My First E-Book
Quick Report
Yearly Posts:
   i) Oct-Nov 2010: 12
   ii) In 2011: 380
   iii) In 2012: 32

You can find my articles and blogs on:
   i) itorian.com
   ii) c-sharpcorner.com
   iii) dotnetfunda.com
   iv) codeproject.com

Greatest Hits
People I Follow
Disclaimer
This is my personal website and the opinions I have expressed here is my own. For any accuracy I recommend to visit official websites like MSDN for Microsoft. I developed this website to share my technical skills.

Query Expression vs Dot Notation in LINQ (a concise talk)
[In this quick post I am discussing (concise talk) over comparison between Dot Notation and Query Expression in LINQ.]
Abhimanyu Kumar Vatsa

IT Faculty || I blog on Microsoft Technologies || Mindcracker MVP || Founder of ITORIAN.COM
   

Introduction

We can see there are two or more ways to achieve the same results in LINQ that is by using dot notation and query expression.

Look at the sample examples.

Query Expression Example

var results = from product in products

                orderby product.Price descending

                select new

                {

                    product.Name,

                    product.Price

                };

 

int count = 0;

// print out the names

foreach (var p in results)

{

    Console.WriteLine("Item: {0}, Cost: {1}", p.Name, p.Price);

    if (++count == 3)

    {

        break;

    }

}

 

Dot Notation Example

var results = products

                        .OrderByDescending(e => e.Price)

                        .Take(3)

                        .Select(e => new { e.Name, e.Price });

 

foreach (var p in results)

{

    Console.WriteLine("Item: {0}, Cost: {1}", p.Name, p.Price);

}

 

The idea is that there are two ways to express the same thing, but which way is the best?

The "dot" notation is usually called Lambda syntax. The first notation goes by a number of names but I usually call it the query syntax.

As I know, in general, the more seasoned (with LINQ) developers migrate towards the Lambda syntax but there are significant exceptions.

Lambda is more concise but performing multiple table joins is a nightmare. Joins are just much cleaner with the query syntax. The flip side is that there are a number of LINQ operations that only exist within the Lambda syntax: Single(), First(), Count() etc.

At the end, use what you feel most comfortable with and realize that as you gain experience, your preference will likely change. There is great value in being able to read both and there will certainly be situations where you have to use a little bit of both. Other situations will lend themselves to one style over the other. In the end, it all gets translated into the same executable code, this is main idea. Go as you wish.
 
Happy Coding !!



Join us on Facebook. Join us on Twitter.

   



Approved Comments

Mona
Mona: I love the way you represent the ideas. thanks buddy
Author: you welcome mona!!