LINQ: Dot Notation vs Query Expression

19,443

Solution 1

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.

I work on a team of 10 developers and we argue at length about which we should use as a standard. 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.

So, 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 themselve to one style over the other. In the end, it all gets translated into the same executable code.

Solution 2

I use whichever syntax is more readable for my query, on a case-by-case basis.

Where possible, I try to avoid mixing and matching the two, although sometimes it's okay (if it's a single call to First() at the end of a query, for example). Deferred execution means it's just as efficient to use a query expression and assign the result to a variable, and then use dot notation using that variable:

var query = from x in y
            orderby z
            group x by x.Name into groups
            // etc
            select foo;

var page = query.Skip(50).Take(10);

As others have said, query expressions are just translated into "normal" C# 3 without query expressions, so there's no penalty for doing this.

Solution 3

Well, the 'dot' notation can be much shorter. Take:

var result = from p in dc.Products
             where p.Id > 5
             select p;

or:

var result = dc.Products.Where(p => p.Id > 5);

I prefer the latter since it is much shorter, and more readable.

Solution 4

I find Lambda notation neater and more concise. I just find it annoying that if you have a Lambda expression anywhere inside a method call, you can't modify code on the fly in debug mode...

Solution 5

They compile to the same code, or rather first one is first translated to the second and then compiled.

You are right that the difference is that first version is cleaner but more limited. In the second you can for example use already existing delegates, e.g.:

Func<int, bool> isEven = i => i%2 == 0;
Enumerable.Range(10).Where(isEven).ToList().ForEach(Console.WriteLine);
Share:
19,443
Martin Marconcini
Author by

Martin Marconcini

Android developer (Kotlin/Java) and some Flutter (newbie). Did iOS (Swift) for a while, not really up to date with the latest Swift. I'm very interested in doing Rust, but I haven't found a chance to properly learn it and nobody will pay me to do it so... here we are :) I haven't done Objective-C since before ARC. I haven't done C#.NET since .NET 3.5 in 2011. I occasionally use Windows for gaming. I mainly use Linux for everything else (and macOS when I have no choice, which is sometimes the case in some corporate environments). Note to Recruiters: Please don't make me offers that are not 100% remote friendly; I've been working from home since 2001 and I expect to continue to do so, pandemic or not. Coming to an office every now and then or once a week is fine, but it should not be the norm; as a developer, the distractions are too many. I disagree with StackOverflow discontinuing JOBS/Developer Story, so I may not be as active as before on this site.

Updated on June 25, 2022

Comments

  • Martin Marconcini
    Martin Marconcini almost 2 years

    I am beginning to use LINQ in general (so far toXML and toSQL). I've seen that sometimes there are two or more ways to achieve the same results. Take this simple example, as far as I understand both return exactly the same thing:

    SomeDataContext dc = new SomeDataContext();
    
    var queue = from q in dc.SomeTable
            where q.SomeDate <= DateTime.Now && q.Locked != true
            orderby (q.Priority, q.TimeCreated)
            select q;
    
    var queue2 = dc.SomeTable
            .Where( q => q.SomeDate <= DateTime.Now && q.Locked != true )
            .OrderBy(q => q.Priority)
            .ThenBy(q => q.TimeCreated);
    

    The idea is that there are two ways to express the same thing; I understand that the first method has some limitations and that the "dot notation" is more complete, but besides that, are there any other advantages?