LINQ Syntax (Query Syntax & Method Syntax)

  Total Views : 595
  Comments : 0
 
  Keywords : Linq syntax in c# / asp.net with example, Linq query syntax tutorial in with example, Query syntax in linq with example, Query syntax or method syntax in linq with example

Here we will learn linq syntax with examples, linq query syntax with example, linq method syntax with examples.

LINQ Syntax

LINQ is an acronym of “Language Integrated Query” and its main feature is to allows users to write SQL style of queries with in code using query syntax’s.

 

.NET framework (3.5 or Higher) provides set of built in query keywords in LINQ to allow users to write SQL style of queries with in code.

 

There are two ways in which we can write queries in LINQ

 

  • Using Query Syntax
  • Using Method Syntax

LINQ Query Syntax

In LINQ. we write LINQ query by following certain rules regarding the syntax which is quite different from the SQL. We use below LINQ query syntax hierarchy to write a LINQ query.

 

from <variable> in <collection>

<where, joining, grouping, operators etc.> <lambda expression>

<select or groupBy operator> <format the results>

This order is to be followed while writing queries in LINQ. “from” keyword will form the starting point of the LINQ query followed by a user defined variable followed by “in” which actually specifies our source collection or Data source followed by a where clause, if there is a certain condition in the query can be used before the select to filter out the records and select is followed by group by and into clause.

 

The order of the clauses in LINQ query is as below

 

ClauseDescription
From [Identifier]
In [Source Collection]
Let [Expression]
Where [Boolean Expression]
order by [Expression]
Select [Expression]
group by [Expression]
Into [Expression]

The following simple code snippets shows the linq query syntax in detail.

 

C# Code

 

int[] Num = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

IEnumerable<int> result = from numbers in Num

                                            where numbers >3

                                            select numbers;

VB.NET Code

 

Dim Num AsInteger() = {1, 2, 3, 4, 5, 6, 7, 8, 9}

Dim result AsIEnumerable(OfInteger) = From numbers In Num

                                                                Where numbers > 3

                                                                Select numbers

LINQ Query Syntax Example

An example is shown below that uses LINQ. In this example we have an integer array having nine elements. We will print the elements from given array where element value greater than 3 and display them using LINQ.

 

C# Code

 

using System;

using System.Collections.Generic;

using System.Linq;

namespace Linqtutorials

{

class Program

{

static void Main(string[] args)

{

int[] Num = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

//LINQ Query Syntax to Print Numbers Greater than 3

IEnumerable<int> result = from numbers in Num

where numbers >3

select numbers;

foreach (var item in result)

{

Console.WriteLine(item);

}

Console.ReadLine();

}

}

}

VB.NET Code

 

Module Module1

Sub Main()

Dim Num As Integer() = {1, 2, 3, 4, 5, 6, 7, 8, 9}

'LINQ Query Syntax to Print Numbers Greater than 3

Dim result As IEnumerable(Of Integer) = From numbers In Num Where numbers > 3 Select numbers

For Each item In result

Console.WriteLine(item)

Next

Console.ReadLine()

End Sub

End Module

In above example we have an integer array having nine elements and getting values where values greater than 3. We are using LINQ to select the elements from the array.

 

If you observe we have used “from” identifier in the beginning of the query (Refer Ordering table) and declared numbers variable which is user defined and added where condition to get elements numbers value greater than 3 and is used to retrieve the result set with select clause in the later part of the query.

 

And of course we get elements from Num integer array where numbers values greater than 3 which is our data source in this program using “in” keyword.

 

Since the resultant output in LINQ is of type IEnumerable<int> so we declare the result object of type IEnumerable<int>. And to access the elements we have to loop through it using a foreach loop to get the desired output. 

 

Now run the given code and we should get the below output.

 

4

5

6

7

8

9

LINQ Method Syntax

In LINQ, method syntaxes will use extension methods of Enumerable or Queryable static classes.

 

Following is the code snippet of using method syntaxes in applications.

 

C# Code

 

int[] Num = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

//LINQ Method Syntax to Print Numbers Greater than 3

IEnumerable<int> result = Num.Where(n => n > 3).ToList();

VB.NET Code

 

Dim Num As Integer() = {1, 2, 3, 4, 5, 6, 7, 8, 9}

'LINQ Method Syntax to Print Numbers Greater than 3

Dim result As IEnumerable(Of Integer) = Num.Where(Function(n) n > 3).ToList()

If you observe above code snippets we are using extension methods Where and ToList() of IEnumerable class. 

LINQ Method Syntax Example

Following is the example of using method syntax query with IEnumerable<T>.

 

C# Code

 

using System;

using System.Collections.Generic;

using System.Linq;

 

namespace LINQExamples

{

class Program

{

static void Main(string[] args)

{

int[] Num = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

//LINQ Method Syntax to Print Numbers Greater than 3

IEnumerable<int> result = Num.Where(n => n > 3).ToList();

foreach (var item in result)

{

Console.WriteLine(item);

}

Console.ReadLine();

}

}

}

VB.NET Code

 

Module Module1

Sub Main()

Dim Num As Integer() = {1, 2, 3, 4, 5, 6, 7, 8, 9}

'LINQ Method Syntax to Print Numbers Greater than 3

Dim result As IEnumerable(Of Integer) = Num.Where(Function(n) n > 3).ToList()

For Each item In result

Console.WriteLine(item)

Next

Console.ReadLine()

End Sub

End Module

If you observe above example, we used IEnumerable collection extension methods Where and ToList() to query data based on our requirements. Now we will run and see the output that would be like as shown below.

 

4

5

6

7

8

9

This is how we can write queries in LINQ using query syntax and method syntax based on our requirements. 

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User