LINQ Syntax (Query Syntax & Method Syntax)

Here we will learn LINQ syntax with examples, LINQ query syntax with examples, LINQ method syntax with examples.

LINQ Syntax

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.

 

  • Using Query Syntax
  • Using Method Syntax

LINQ Query Syntax

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.

 

from <variable> in <collection>
<where, joining, grouping, operators etc.> <lambda expression>
<select or groupBy operator> <format the results>

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.

 

ClauseDescription
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.

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 As Integer() = {1, 2, 3, 4, 5, 6, 7, 8, 9}
Dim result As IEnumerable(OfInteger) = From numbers In Num
                                       Where numbers > 3
                                       Select numbers

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.

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}
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 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.

 

4
5
6
7
8
9

LINQ Method Syntax

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.

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}
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

The 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}
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

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.

 

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.