Hello Friends, In this article I will discuss with you the two ways in which we can work on the sequences using LINQ in C# and VB.NET. Lambda Syntax and Comprehension syntax are two ways by which we can comprehend and create LINQ queries.
Lambda Syntax in .NET
LINQ in .NET platform was introduced in the .NET 3.0 and since then it has changes the way we work with collections in .NET framework.
The first way we can write the LINQ query is with the help of extension methods present under the Enumerable class in System.Linq and System.Linq.Expression namespace in System.Core assembly.
These methods are applied on the sequence or collection of elements as these methods are build on top of IEnumerable interface.
There are around 40 query operators present in the Enumerable class.
First of all a query is an expression that transforms the input sequence with query operator.
Finally lets write a simple query in the below code example in C#.
public static void QueryOperator() { string[] names = {"Vikram", "Prasad", "Nikhil", "Sumit", "Varun" }; var filteredNames = names.Where(a => a.StartsWith("V")); foreach (var item in filteredNames) { Console.WriteLine(item); } Console.Read(); }
The above code filters the list of names only with the names starting with “V”. I have provided a simple Lambda predicate which checks the conditions.
“Where” is one of the extension method for the collection.
Comprehension Queries in LINQ
The other way we can write a LINQ query in using comprehension syntax.
Lets write the above query using comprehension syntax first and then we will discuss about the syntax.
public static void QueryOperator() { string[] names = {"Vikram", "Prasad", "Nikhil", "Sumit", "Varun" }; var filteredNames = from name in names where name.StartsWith("V") select name; foreach (var item in filteredNames) { Console.WriteLine(item); } Console.Read(); }
Now as we can see I have replaced the Extension methods with some wired looking code to get the filteredNames.
The above syntax is known as comprehension Syntax. It always starts with from clause and ends with select or group clause.
The from clause declares an iteration variable (n in the above example) and I think you can relate it with foreach keyword.
Finally the Compiler processes the comprehension syntax by translating them to lambda syntax. Any query which you can write in Comprehension syntax can be written in Lambda syntax as well.
Advantages of Comprehension Syntax
Comprehension syntax has couple of more advantages as compared to Lambda Syntax as discussed below. And comprehension syntax is much simpler for queries for which involve below conditions:
- A let clause for introducing new variable alongside the iteration variable.
- SelectMany, Join or GroupJoin followed by an outer iteration variable reference.
Both of these above points I will discuss in my future articles.
Mixed Syntax Queries
Finally we can make use of both of the ways to write the queries using LINQ in C#.
Below is an example of the same.
string[] names = {"Vikram", "Prasad", "Nikhil", "Sumit", "Varun" }; var filteredNames = (from name in names where name.StartsWith("V") select name).Count();
The first of the query is comprehension query and the we are using Count() extension method to get the count of the elements.
Leave a Reply