Here we will learn LINQ syntax with examples, LINQ query syntax with examples, LINQ method syntax with examples.
LINQ is an acronym of “Language Integrated Query” and its main feature is to allow users to write SQL style of queries within code using query syntax.
.NET framework (3.5 or Higher) provides a set of built-in query keywords in LINQ to allow users to write SQL style of queries within code.
There are two ways in which we can write queries in LINQ.
We write LINQ queries by following specific rules, and the syntax is quite different from the SQL. To write a LINQ query, we need to follow the syntax hierarchy mentioned below.
This order is to follow while writing the queries in LINQ. “from
” keyword will form the starting point of the LINQ query followed by a user defined variable
followed by “in
” which specifies our source collection or Data source followed by a where
clause, if there is a specific condition in the query that can be used before the select to filter out the records and select is followed by group by and into the clause.
The order of the clauses in the LINQ query will be as shown below.
Clause | Description |
---|---|
From | [Identifier] |
In | [Source Collection] |
Let | [Expression] |
Where | [Boolean Expression] |
order by | [Expression] |
Select | [Expression] |
group by | [Expression] |
Into | [Expression] |
The following are the code snippets of linq query syntax in c# and vb.net.
Following are the examples that will show how to use the LINQ query syntax in our applications. Here, we are using an integer array with nine elements, and we will print the array elements whose value is greater than 3.
In the above example, we define an integer array (Num) with nine elements and print only the array elements whose value is greater than 3.
If you observe the above examples, we used the “from” identifier at the beginning of the query (Refer Ordering table). We declared a numbers variable that is user-defined and added where condition to get the elements whose value is greater than 3. We used the numbers variable with a select clause to retrieve the result set.
And of course, we will get the elements from Num integer array where numbers value greater than 3, which is our data source in this program using the “in” keyword.
Since the resultant output in LINQ is of type IEnumerable<int>, we declared the result type as IEnumerable<int>. To access elements from the resultant object, we used a foreach loop to get the desired output.
When we execute the above examples, we will get the following result.
In LINQ, method syntaxes will use the extension methods of Enumerable or Queryable static classes.
The following are the code snippet of using method syntaxes in c# and vb.net.
If you observe above code snippets we are using extension methods Where and ToList() of IEnumerable class.
The following are the examples of using method syntax query with IEnumerable<int> in c# and vb.net.
The above example shows that we used IEnumerable collection extension methods (Where and ToList()) to query the data based on our requirements.
When we execute the above program, we will get the following result.
This is how we can write the queries in LINQ using query syntax and method syntax based on our requirements.