This article is currently in the process of being translated into Vietnamese (~22% done).
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>()
{
"John Doe",
"Jane Doe",
"Jenna Doe",
"Joe Doe"
};
// 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.
Lambda Expressions
Trong ví dụ về cú pháp phương thức, bạn có thể chú ý một số thứ bạn không thấy trước đó, như là tham số trong phương thức Where(). Có dạng như sau:
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.
Rất đơn giản. Biểu thức Lambda có phần bên trái và bên phải, ở giữa là toán tử => (không nhầm với "lớn hơn hoặc bằng", có dạng như sau: >=).)
Ở phía bên trái, chúng ta có tham số đầu vào - có thể là nhiều tham số nhưng trong ví dụ của chúng ta, chỉ có một, là biến name. Trong trường hợp này, nó có thể tới từ phương thức Where(), bằng cách duyệt qua danh sách tên và chúng ta sẽ chọn lấy tên phù hợp. Tôi chọn "name" vì tìm trong list của name nhưng bạn cũng có thể gọi nó là "x", "z", "foobar" hay bất kỳ cái gì khác.
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.
Summary
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.