LINQ: Query Syntax vs. Method syntax
In the previous article, we got a quick glimpse of how both LINQ syntaxes look, but let's just get them on the table again right next to each other, so you are sure of what we'll be discussing in this article:
var listOfNames = new List<string>()
// Query syntax
var qNames = from name in listOfNames where name.Length <= 8 select name;
// Method syntax
var mNames = listOfNames.Where(name => name.Length <= 8);
While the two lines will accomplish the exact same thing, the syntactical difference is quite clear: The query syntax looks more like other query languages, e.g. SQL and less like a regular C# statement. The method syntax, on the other hand, looks very much like the rest of the C# code you have seen in this tutorial.
So, the biggest difference is definitely just the syntax and therefore, you will see both variations used when reading C# articles and finding answers to questions you might have. It seems that the query syntax is slightly more popular because some people find it easier to read than the method syntax - these might be people who are used to expressing data retrieval operations in SQL anyway. On the other hand, if you have never used a query language like SQL before, but you have some C# experience, then you might find the method syntax easier to read. Also, for at least a couple of operations, like counting the items in the result of the query, you will have to use the method syntax.
In the method syntax example, you might notice something that you haven't seen before, as a parameter to the Where() method. It looks like this:
name => name.Length <= 8
This is called a Lambda Expression and while it's not a specific piece of LINQ functionality, it's used A LOT in the world of LINQ. Therefore you will also see it a lot in the following articles, and while I will go into greater details about Lambda Expressions in another article, this is a good time to get a very basic understanding of how they work.
It's actually quite simple. A Lambda Expression has a left side and a right side, divided by the => operator (not to be confused with the "greater than or equal to"-operator, which looks like this: >=).
On the left side, we have the input parameter(s) - there can be several, but in our example, there's just one, which is the name variable. In this case, it comes from the Where() method, from iterating over our list of names and we get to pick the name. I chose "name", because that's what you'll find in a list of names, but you could also call it "x", "z", "foobar" or anything else.
On the right side, we have the expression/statement part. This is where we generate the expected result - in this case, a boolean value telling the Where() method whether to include the name in question or not. We answer that with an expression, saying that we want to include the name if it has a length of 8 (or less) characters. We can (but are not required to) use the input (left side) to determine this. Again, we refer to it as "name", but we could have called it "x" instead - in that case, the statement would have looked like this:
var mNames = listOfNames.Where(x => x.Length <= 8);
Lambda Expressions are about more than just LINQ queries, but as mentioned, they are important when using LINQ - you will see that in the next articles. If you want to know more about Lambda Expressions in general, have a look elsewhere in this tutorial where the topic will be covered.
LINQ comes in two syntactical flavors: The Query and the Method syntax. They can do almost the same, but while the query syntax is almost a new language within C#, the method syntax looks just like regular C# method calls. In this tutorial, we will mostly be using the method syntax, when we discuss all the possible operations available with LINQ in the next articles.