Home » LINQ Syntax

LINQ Syntax

Before proceeding to the LINQ Query Syntax, we will discuss some basic terms related to LINQ Syntax:

The requirement for writing the LINQ Query

To write the LINQ Query, we need the following three things:

  1. Data Source (in-memory objects, SQL, XML)
  2. Query
  3. Execution of the Query

What is a Query?

A query is nothing but a set of instructions. Queries are applied to the data source (i.e., in-memory object, SQL, XML, etc.) to perform the operations (i.e., CRUD operations) and show the shape of the output from that Query. This means that the Query is not responsible for what will be the output; instead, it is responsible for the shape of the output.

Each Query is a combination of three things; they are:

  1. Initialization(to work with a particular data source)
  2. Condition(where, filter, sorting condition)
  3. Selection (single selection, group selection or joining)

LINQ is an acronym of “Language Integrated Query.” The main feature is to allow the users to write the queries in the style of SQL queries within the code using the query syntaxes.

The .NET framework provides the set of built-in query keywords in LINQ to allow the users to write the SQL style queries.

LINQ has three ways to write the queries:

  • Using Query Syntax
  • Using Method Syntax
  • Using Mixed Syntax

LINQ Query Syntax

LINQ is one of the easiest ways to write the complex LINQ Queries in an accessible and readable format. The syntax of this type of Query is much similar to SQL queries.

Syntax of LINQ is as:

LINQ Syntax

In LINQ, we write the LINQ query by following certain rules. The syntax is different from the SQL. For writing the Query in LINQ, there is a need to follow the syntax hierarchy like as:

We will follow this order when we are writing the queries in LINQ. The starting point of LINQ will start from the “from” keyword, which is followed by a user-defined variable followed by in which specifies the source of the collection or reference of the data, which is followed by a where clause. If there is a particular query that can be used before the select ion to filter the record and select is followed by and group by into the clause.

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

Clauses Description
From [Identifier]
In [Source Collection]
Let [Expression]
Where [Boolean Expression]
order by [Expression]
Select [Expression]
group by [Expression]
into [Expression]

Code snippets of LINQ Query in C#

LINQ Query Syntax in C#

Now we will understand how to use the LINQ Query syntax through the following example:

Example: We have an integer list, and we need to write a LINQ query that will return all the integers, which are higher than 5. Here we will create a console application.

Example Using Query Syntax

Now we will run the application, and it will display the values 6, 7, 8,9,10, as shown below in the output in the console window.

Output

LINQ Syntax
LINQ Syntax

LINQ Method Syntax

Method Syntax becomes the most popular nowadays to write LINQ queries. It uses a lambda expression to define the condition for the Query. Method Syntax is easy to write simple queries to perform the read-write operations on a particular data source. For the complex queries, Method Syntaxes are a little hard as compared to the Query Syntax.

In this approach, the LINQ Query is written by using the multiple methods and combining them with a dot (.).

For this approach, the syntax is:

LINQ Syntax

Now we are going to rewrite the same example by using the LINQ method syntax:

Output

LINQ Syntax

LINQ Mixed Syntax

LINQ Mixed Syntax is a combination of both Query and MethodSyntax.

LINQ Syntax

Example: Now, we are changing the requirement. First, we need to filter the list where the value is higher than five, and then we need to calculate the sum.

Now we run the application, and it will display the sum is 40, as shown below in the output in the console window.

Output

LINQ Syntax


You may also like