LINQ Syntax – Quick Guide

Topics

  • What is a QUERY?
  • LINQ Query Syntax
  • LINQ Method Syntax

What is a Query?

A query is an expression which is used to perform some task.

As a developer, you’re expected to learn different types of languages to be able to query different types of data sources like SQL for a relational database, XQuery for XML, etc

How good will it be, if we can have a single language to do it all?

LINQ is the solution for that, using the LINQ query you can access any type of data source like Objects, XML document, SQL database, etc. and perform a variety of operations.

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

  • Using Query Syntax

  • Using Method Syntax


LINQ Query Syntax

Query Syntax looks similar to SQL queries and it allows the developers to write it in C# and VB.Net without using quotes as we do it in SQL queries(It actually reduces the chances of any runtime error).

Syntax:

From UserDefinedVariable in OurDataSource
Where, joining, grouping, etc. <OurLambdaExpression>
Select or groupBy operator <OurResults>

Let’s understand the above syntax

The query starts with “From” keyword followed by a “user-defined-variable”, then followed by an “In” keyword and then “Our Collection” which is our data source.
Then we use the “Where” clause to apply some filter conditions to the query and then followed by the “Select” keyword to take out the value that met the condition.

LINQ Query Syntax Example using C#

Let’s write a LINQ query to fetch the numbers with a value of less than 50.

Our Data Source:
int[] myNum_array = { 10, 20, 30, 40, 50, 60, 70, 80, 90 }

Creating our LINQ Query:
IEnumerable myResults = from number in myNum_array
where number <50
select number;

Finally, execute the query by using a foreach loop:
foreach (var item in myResults)
{
Console.WriteLine(item);
}

Following is the full code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LinqExamplesCSharp
{
class Program
{
static void Main(string[] args)
{
int[] myNum_array = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };

//LINQ Query Syntax to Print Numbers less than 50
IEnumerable myResults = from number in myNum_array
where number <50
select number;

foreach (var item in myResults)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
}
}

Output:
10
20
30
40

LINQ Query Syntax Example using VB.Net

Module Module1
Sub Main()

Dim myNum_array As Integer() = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}
'LINQ Query Syntax to Print Numbers less than 50
Dim myResults As IEnumerable(Of Integer) = From number In myNum_array Where number < 50 Select number

For Each item In myResults
Console.WriteLine(item)
Next
Console.ReadLine()
End Sub
End Module

Output:
10
20
30
40

LINQ Method Syntax

Method Syntax is simply using already available extension methods of the Enumerable class to obtain our desired output.

Syntax:

OurDatasource.<Where,ToList,Contains, etc..>

Let’s understand the above syntax

Here we are using standard query operators like Select, Tolist, Contains, Where, GroupBy, Join, Sum, Max, etc.
Here you can directly call them without using query syntax.

LINQ Method Syntax Example using C#

Let’s write a LINQ query to fetch the numbers with a value of less than 50.

Our Data Source:
int[] myNum_array = { 10, 20, 30, 40, 50, 60, 70, 80, 90 }

Creating our LINQ Method Query:
IEnumerable<int> myResults = myNum_array.Where(n => n < 50).ToList()

Finally, execute the query by using a foreach loop:
foreach (var item in myResults)
{
Console.WriteLine(item);
}

Following is the full code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LinqExamplesCSharp
{
class Program
{
static void Main(string[] args)
{
int[] myNum_array = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };

//LINQ Method Syntax to Print Numbers less than 50
IEnumerable<int> myResults = myNum_array.Where(n => n < 50).ToList();

foreach (var item in myResults)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
}
}

Output:
10
20
30
40

LINQ Method Syntax Example using VB.Net

Module Module1
Sub Main()

Dim myNum_array As Integer() = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}
'LINQ Method Syntax to Print Numbers less than 50
Dim myResults As IEnumerable(Of Integer) = myNum_array.Where(Function(n) n > 3).ToList()

For Each item In myResults
Console.WriteLine(item)
Next

Console.ReadLine()
End Sub
End Module

Output:
10
20
30
40

Previous topic : LINQ Introduction
Next Topic: LINQ Lambda Expression

For more articles on LINQ, Click here

Leave a Reply

%d bloggers like this: