Sign in
Your Position: Home >Packaging & Printing >What is LINQ

What is LINQ

Nov. 28, 2023
  • 284
  • 0
  • 0

What is LINQ?

Language-Integrated Query (LINQ) is a powerful set of technologies based on the integration of query capabilities directly into the C# language. LINQ Queries are the first-class language construct in C# .NET, just like classes, methods, events. The LINQ provides a consistent query experience to query objects (LINQ to Objects), relational databases (LINQ to SQL), and XML (LINQ to XML).

LINQ (Language Integrated Query) is uniform query syntax in C# and VB.NET to retrieve data from different sources and formats. It is integrated in C# or VB, thereby eliminating the mismatch between programming languages and databases, as well as providing a single querying interface for different types of data sources.

For example, SQL is a Structured Query Language used to save and retrieve data from a database. In the same way, LINQ is a structured query syntax built in C# and VB.NET to retrieve data from different types of data sources such as collections, ADO.Net DataSet, XML Docs, web service and MS SQL Server and other databases.

LINQ Usage

LINQ queries return results as objects. It enables you to uses object-oriented approach on the result set and not to worry about transforming different formats of results into objects.

The following example demonstrates a simple LINQ query that gets all strings from an array which contains 'a'.

Example: LINQ Query to Array

// Data source

string

[] names = {"Bill", "Steve", "James", "Mohan" };

// LINQ Query

var

myLinqQuery =

from

name

in

names

where

name.Contains('a')

select

name;

// Query execution

foreach

(

var

name

in

myLinqQuery)

Console

.Write(name + " ");

In the above example, string array names is a data source. The following is a LINQ query which is assigned to a variable myLinqQuery.

from

name

in

names

where

name.Contains('a')

select

name;

The above query uses query syntax of LINQ. You will learn more about it in the Query Syntax chapter.

You will not get the result of a LINQ query until you execute it. LINQ query can be execute in multiple ways, here we used foreach loop to execute our query stored in myLinqQuery. The foreach loop executes the query on the data source and get the result and then iterates over the result set.

Thus, every LINQ query must query to some kind of data sources whether it can be array, collections, XML or other databases. After writing LINQ query, it must be executed to get the result.

Learn why should we use LINQ in the next chapter.

LINQ Query Syntax

There are two basic ways to write a LINQ query to IEnumerable collection or IQueryable data sources.

  1. Query Syntax or Query Expression Syntax
  2. Method Syntax or Method Extension Syntax or Fluent

Query Syntax

Query syntax is similar to SQL (Structured Query Language) for the database. It is defined within the C# or VB code.

LINQ Query Syntax:

from <range variable> in <IEnumerable<T> or IQueryable<T> Collection>

<Standard Query Operators> <lambda expression>

<select or groupBy operator> <result formation>

The LINQ query syntax starts with from keyword and ends with select keyword. The following is a sample LINQ query that returns a collection of strings which contains a word "Tutorials".

Example: LINQ Query Syntax in C#

// string collection

IList

<

string

> stringList =

new

List<

string

>() {

"C# Tutorials"

,

"VB.NET Tutorials"

,

"Learn C++"

,

"MVC Tutorials"

,

"Java"

};

// LINQ Query Syntax

var

result =

from

s

in

stringList

where

s.Contains(

"Tutorials"

)

select

s;

The following figure shows the structure of LINQ query syntax.

LINQ Query Syntax

Query syntax starts with a From clause followed by a Range variable. The From clause is structured like "From rangeVariableName in IEnumerablecollection". In English, this means, from each object in the collection. It is similar to a foreach loop: foreach(Student s in studentList).

After the From clause, you can use different Standard Query Operators to filter, group, join elements of the collection. There are around 50 Standard Query Operators available in LINQ. In the above figure, we have used "where" operator (aka clause) followed by a condition. This condition is generally expressed using lambda expression.

LINQ query syntax always ends with a Select or Group clause. The Select clause is used to shape the data. You can select the whole object as it is or only some properties of it. In the above example, we selected the each resulted string elements.

In the following example, we use LINQ query syntax to find out teenager students from the Student collection (sequence).

Example: LINQ Query Syntax in C#

// Student collection

IList

<

Student

> studentList =

new

List<

Student

>() {

new

Student

() { StudentID = 1, StudentName =

"John"

, Age = 13} ,

new

Student

() { StudentID = 2, StudentName =

"Moin"

, Age = 21 } ,

new

Student

() { StudentID = 3, StudentName =

"Bill"

, Age = 18 } ,

new

Student

() { StudentID = 4, StudentName =

"Ram"

, Age = 20} ,

new

Student

() { StudentID = 5, StudentName =

"Ron"

, Age = 15 } };

// LINQ Query Syntax to find out teenager students

var

teenAgerStudent =

from

s

in

studentList

where

s.Age > 12 && s.Age < 20

select

s;

Example: LINQ Query Syntax in VB.Net

// Student collection

Dim

studentList =

New

List

(Of

Student

) From {

New

Student

()

With

{.StudentID = 1, .StudentName =

"John"

, .Age = 13},

New

Student

()

With

{.StudentID = 2, .StudentName =

"Moin"

, .Age = 21},

New

Student

()

With

{.StudentID = 3, .StudentName =

"Bill"

, .Age = 18},

New

Student

()

With

{.StudentID = 4, .StudentName =

"Ram"

, .Age = 20},

New

Student

()

With

{.StudentID = 5, .StudentName =

"Ron"

, .Age = 15} }

// LINQ Query Syntax to find out teenager students

Dim

teenAgerStudents

As

IList

(Of

Student

) = (

From

s

In

studentList _

Where

s.Age > 12

And

s.Age < 20 _

Select

s).ToList()

Points to Remember :

  1. As name suggest, Query Syntax is same like SQL (Structure Query Language) syntax.
  2. Query Syntax starts with from clause and can be end with Select or GroupBy clause.
  3. Use various other opertors like filtering, joining, grouping, sorting operators to construct the desired result.
  4. Implicitly typed variable - var can be used to hold the result of the LINQ query.

What is LINQ

LINQ Query Syntax

Comments
Comments

0/2000

Get in Touch
Guest Posts