Join Olivia Chiu Stone for an in-depth discussion in this video Lambda expressions, part of LINQ with C# Essential Training.
- Up to this point in the course, I've demonstrated all the link queries using a syntax called query comprehension syntax. It's a syntax format that makes your queries easy to read and understand. However, at the end of the day, these queries need to be translated into method calls for the .NET common language runtime, or CLR, to interpret them. These methods call the extension methods that the link library provides, and you can call them directly by using the method syntax which is also known as lambda syntax. The top query pulls customers who are in Oregon, like I've done before.
The lower query performs the same function using the lambda syntax. Notice, there's no longer the from or select clauses. This is because the lambda syntax is calling the extension methods provided by the link extension directly. Instead of using from, the sequence to be queried is introduced just after the equals sign with its name and the query expression is called like a .method. To define and shape the query, a lambda expression is used. If you are not familiar with the lambda expression, it is a way to quickly write a short in-line function.
In C#, the lambda operator is written as an equals sign followed by a greater than symbol. This can be read as "goes to" and helps make the code easier to read. The c, to the left of the operator can be seen as the input variable. In a link query, it is treated as a range variable. To the right of the lambda operator is a criteria to evaluate the variable. In this case, the state property of c is checked to see if it's equal to OR. All queries that can be written in comprehension syntax can also be written in lambda syntax.
However, the SelectMany, Join, and GroupJoin queries are generally simpler to write with comprehension syntax. In general, comprehension syntax is easier to read. It always starts with a from clause and always ends with a select or group clause. So, whenever someone looks at a query, it's fairly easy to understand what is going on. However, there are additional link clauses that I've not talked about yet that can only be used with a lambda syntax. These clauses don't have an equivalent implementation in comprehension syntax.
Deciding which syntax to use for a query is the mix of personal preference and what you want to accomplish with a query. It's good to know and understand both types of syntax. There may be some situations where it's beneficial to use a mix of both comprehension and lambda syntax in the same program. This example program called 06_01_Lambda_Syntax_start contains a query written in the comprehension syntax using the where operator to filter the results. Now, I'll modify this query to be written using the lambda syntax.
The from operator is no longer needed to specify the data source. Instead, I just reference it directly after the equals sign. And since the extension methods are used directly, I type .Where and add the lambda expression to specify the filtering criteria, which in this case is looking for the state property that equals to OR. And that's it. I don't need to select operator either, since I just want the values returned from the where query.
And I run this. And I get the exact same list as I had before, using the comprehension syntax.
In LINQ with C# Essential Training, engineer Olivia Chiu introduces techniques for querying, updating, and transforming data with LINQ. She covers standard queries—such as finding overlaps in two datasets and creating hierarchies—as well as complex chained queries. She also shows how to group and join LINQ queries with lambda expressions, and use LINQ to query SQL databases and XML documents. Last but not least, Olivia provides tips for optimizing the performance of your queries.
- Creating and executing a LINQ query
- Returning results
- Changing the data source type
- Performing standard queries
- Working with lambda expressions
- Chaining and complex queries
- Querying SQL and XML
- Performing tree queries
- Using sequences, elements, and scalars
- Allocating memory