LINQ query syntax and query expression
With built-in LINQ extension methods and lambda expressions, Visual Studio allows us to write SQL-like statements in C# when invoking these methods. The syntax of these statements is called LINQ query syntax and the expression in query syntax is called a query expression.
For example we can change this statement:
var veges6 = products.Where(p => p.ProductName.Contains("vegetable"));
to the following query statement by using the new LINQ query syntax:
var veges7 = from p in products where p.ProductName.Contains("vegetable") select p;
In the above C# statement we can directly use the SQL keywords, select
, from
, and where,
to "query" an in-memory collection list. In addition to the in-memory collection lists we can use the same syntax to manipulate data in XML files, in the dataset, and in the database. In the following chapters we will see how to query a database using LINQ to Entities.
Combined with the anonymous data type, we...