C#

Language-Integrated Query (LINQ) in C#

Language-Integrated Query (LINQ) in C# allows us to formulate the queries in C# language itself rather than using the syntax of SQL (Structured Query Language).

The native data querying capabilities to the .NET languages have become part of the .NET Framework 3.5 with Visual Studio 2008 in the form of Language-Integrated Query (LINQ). With LINQ, now programmers no longer need to work with several different query languages like SQL to access data. Instead, only one language such as C# is sufficient since now it has all such capabilities of data access and manipulation like any other query language.

LINQ provides a familiar syntax of programming language for writing database queries. Moreover, the syntax remains same for all kinds of data sources whether it is an object, a relational database, or an XML data source.

Language-Integrated Query (LINQ) in C#

The following section shows the data sources used in LINQ and the type of operations that we can perform using LINQ.

What Type of Data Sources can be Used with LINQ?

Since LINQ allows the common syntax for queries on different data sources, it makes it easier to update the queries when the data sources are changes. As a matter of fact, we can use the following data sources with LINQ

  • SQL Databases
  • Datasets
  • Entity Framework
  • XML Data Source
  • Objects of the Collection classes that implement IEnumerable or IEnumerable<T> interface such as List<T> or ArrayList

What Kind of Query Operations can be Performed using LINQ?

In fact, LINQ provides the capability to a .NET programming language to perform all kinds of query operations that any general-purpose query language supports. In short, you can perform the following types of query operations with LINQ.

  • Selection
  • Projection
  • Filtering
  • Ordering (Sorting)
  • Join
  • Grouping
  • Aggregate Operations such as Min, Max, Count, Sum, and Average

How to Write a Query using LINQ?

Basically, a LINQ query contains a number of clauses and starts with the from clause that specifies along with the data source, a range-variable to represent each element in the data source. The from clause is followed by the select clause that indicates the type of values returned by the query. However, in-between you can also include a where clause for filtering the values.

The following example shows a simple LINQ query on an array as the data source. As shown below, double_array is an array of double data type values that the query is using as the data source. Also, the query has a range variable called d. Further, the select clause followed by the from clause is fetching all values from the data source.

using System;
using System.Linq;
namespace ConsoleApp53
{
    class Program
    {
        static void Main(string[] args)
        {
            double[] double_array = {1.2,-4.5, 9.8, 14.7, -10.8 };
            //Formulating Query
            var q1 = from d in double_array
                     select d;
            //Executing Query
            foreach (double d1 in q1)
                Console.Write(d1 + " ");
            Console.WriteLine();
        }
    }
}

Output

Language-Integrated Query (LINQ) in C#
Language-Integrated Query (LINQ) in C#

An Example of Filtering

The following query shows an example of filtering using the where clause to retrieve positive values from the data source.

using System;
using System.Linq;
namespace ConsoleApp53
{
    class Program
    {
        static void Main(string[] args)
        {
            double[] double_array = {1.2,-4.5, 9.8, 14.7, -10.8 };
            //Formulating Query
            var q1 = from d in double_array
                     where d>0
                     select d;
            //Executing Query
            foreach (double d1 in q1)
                Console.Write(d1 + " ");
            Console.WriteLine();
        }
    }
}

Output

Filtering using the where Clause
Filtering using the where Clause

Summary

Basically, the Language-Integrated Query (LINQ) in C# provides us a way to write the queries using the syntax of a .NET Framework language. Therefore, programmers need not use different query languages for different data sources such as databases, collections, and XML documents. Hence, use of LINQ increases the productivity of developers and the applications become easier to maintain.

Also, LINQ is very versatile and you can write any kind of query that is possible with database query languages. Moreover, the queries are reusable since first we can write queries and later execute them. Besides, it provides strict type checking also.


Further Reading

Selection Sort in C#

Insertion Sort in C#

Bubble Sort in C#

How to Create Instance Variables and Class Variables in Python

Comparing Rows of Two Tables with ADO.NET

Example of Label and Textbox Control in ASP.NET

One Dimensional and Two Dimensuonal Indexers in C#

Private and Static Constructors in C#

Methods of Array Class

Anonymous Functions in C#

Programs to Find Armstrong Numbers in C#

Matrix Multiplication in C#

One Dimensional and Two Dimensional Indexers in C#

Static Class Example in C#

Rotating an Array in C#

Generic IList Interface and its Implementation in C#

Recursive Binary search in C#

C# Practice Questions

Creating Navigation Window Application Using WPF in C#

Find Intersection Using Arrays

An array of Objects and Object Initializer

Performing Set Operations in LINQ

Using Quantifiers in LINQ

Data Binding Using BulletedList Control

Grouping Queries in LINQ

Generic Binary Search in C#

Understanding the Quantifiers in LINQ

Join Operation using LINQ

Deferred Query Execution and Immediate Query Execution in LINQ

Examples of Query Operations using LINQ in C#

An array of Objects and Object Initializer

Language-Integrated Query (LINQ) in C#

How Data Binding Works in WPF

Examples of Connected and Disconnected Approach in ADO.NET

New Features in C# 9

IEnumerable and IEnumerator Interfaces

KeyValuePair and its Applications

C# Root Class – Object

Access Modifiers in C#

Learning Properties in C#

Learning All Class Members in C#

Examples of Extension Methods in C#

How to Setup a Connection with SQL Server Database in Visual Studio

Understanding the Concept of Nested Classes in C#

LINQ To SQL Examples

A Beginner’s Tutorial on WPF in C#

Explaining C# Records with Examples

Everything about Tuples in C# and When to Use?

Creating Jagged Arrays in C#

Linear Search and Binary search in C#

Learning Indexers in C#

Object Initializers in C#

Examples of Static Constructors in C#

When should We Use Private Constructors?

C# Basic Examples

IEqualityComparer Interface

You may also like...

1 Comment

  1. Wow that was strange. I just wrote an extremely long comment but after I clicked submit my comment didn’t appear. Grrrr… well I’m not writing all that over again. Anyway, just wanted to say superb blog!

Leave a Reply

Your email address will not be published. Required fields are marked *