Published on 23-07-2015

Should I use Lambda or Query Syntax?

I sometimes hear developers ask, whether they should use Lambda expressions or the more SQL like Query syntax. In this post we will try to find an answer to this interesting question.

There are basically two different paths you can follow when you write LINQ queries. You can either use the so called Lambda expressions, which are based on operators, and have a method syntax. Or you can use Query expressions, which have an SQL like syntax.

static void Main(string[] args)
{
    SimpleLambda();
    SimpleQuery();

    Console.ReadLine();
}

static void SimpleLambda()
{
    int[] numbers = { 7, 9, 5, 3, 6};

    // The Lambda Expression way of writing LINQ
    var res = numbers.Where(n => n > 5);

    foreach (var n in res)
        Console.WriteLine(n);
}

static void SimpleQuery()
{
    int[] numbers = { 7, 9, 5, 3, 6 };

    // The Query Expression way of writing LINQ
    var res = from n in numbers
                where n > 5
                select n;

    foreach (var n in res)
        Console.WriteLine(n);
}

// Output from SimpleLambda method:
// 7
// 9
// 6
// Output from SimpleQuery method:
// 7
// 9
// 6

Although the two methods look very different, they are both 100% LINQ, and sends the exact same output to screen. They also produce the exact same IL (Intermediate Language), which is what the compiler converts the code into. To prove this, compare the below IL for each of the two methods SimpleLambda() and SimpleQuery() respectively.

Intermediate Language for Lambda and Query code.
Figure 1: ILDASM.exe used to demonstrate that Lambda and Query expressions generate the same code. You see the method names in the window title.

In fact, what happens behind the scene when a Query expression is compiled, is that it is first translated into a Lambda expression, and should therefore always produce the same IL. Thus Query expressions are “just” what some will call syntactic sugar.

Some LINQ queries can only be written with Lambda expressions. Take Average() for example, which only exists with Lambda method syntax, and not with Query expressions. Well, that goes for C# developers anyway. Because the availability of Query expressions may differ between .NET languages, and you will find that a Query expression for Average() does in fact exist for VB.NET (see https://msdn.microsoft.com/en-us/library/bb546138.aspx).

You may mix Query and Lambda syntax in your LINQ queries if you so desire. Check out this code example, which use Query syntax to find numbers from array which are greater than 5, and then calculates the average value with a Lambda expression.

static void Mix()
{
    int[] numbers = { 1, 3, 5, 7, 9  };

    var res = (from n in numbers
                where n > 5
                select n).Sum();

    Console.WriteLine("Sum: {0}", res);
}

So if Lambda and Query expressions are semantically identical, are you better off using one instead of the other? Well, some people find Query expressions easier to read because they are already familiar with SQL syntax. Others prefer Lambda for its method approach, which is more flexible and complete (as we saw earlier). Many developers have a preference for either Lambda or Query expressions, but what is right for one person, is not necessarily right for someone else. I hope this short tutorial will help you decide, which approach is better for you.