LINQ Syntax (Query Syntax & Method Syntax)

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 the certain rules and the syntax is quite different from the SQL. To write a LINQ query, we need to follow the syntax hierarchy like as mentioned below.

 

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 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 actually specifies our source collection or Data source followed by a where clause, if there is a certain 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 clause.

 

The order of the clauses in LINQ query will be like as shown below.

 

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

Following are the code snippets of linq query syntax in c# and vb.net.

LINQ Query Syntax in C#

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

IEnumerable<int> result = from numbers in Num

                          where numbers >3

                          select numbers;

LINQ Query Syntax in VB.NET

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

Following are the examples will show how to use the LINQ query syntax in our applications. Here, we are using an integer array which is having nine elements and we will print the array elements whose value is greater than 3.

LINQ Query Syntax Example in C#

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();

}

}

}

LINQ Query Syntax Example in VB.NET

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 the above example, we define an integer array (Num) with nine elements and printing only the array elements whose value is greater than 3.

 

If you observe the above examples, we used “from” identifier at the beginning of the query (Refer Ordering table) and declared numbers variable which is user-defined and added where condition to get the elements whose value is greater than 3 and we used numbers variable with 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 “in” keyword.

 

Since the resultant output in LINQ is of type IEnumerable<int> so we declared the result type as IEnumerable<int> and 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 result like as shown below.

 

4

5

6

7

8

9

LINQ Method Syntax

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

 

Following are the code snippet of using method syntaxes in c# and vb.net.

LINQ Method Syntax in C#

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();

LINQ Methos Syntax in VB.NET

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 are the examples of using method syntax query with IEnumerable<int> in c# and vb.net.

LINQ Method Syntax Example in C# 

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();

}

}

}

LINQ Method Syntax Example in VB.NET

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 the above example, 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 result like as shown below.

 

4

5

6

7

8

9

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

Topics Covered