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


Related Topics

A Beginner’s Tutorial on WPF in C#

Everything about Tuples in C# and When to Use?

Linear Search and Binary Search in C#

Creating Jagged Arrays in C#

Learning Indexers in C#

Understanding Method Parameter Modifiers in C#

Object Initializers in C#

Examples of Static Constructors in C#

When should We Use Private Constructors?

C# Practice Questions

C# Basic Examples

Private and Static Constructors in C#

Constructors in C#

C# Arrays

C# Examples

How to Create a C# Console Application

Creating Navigation Window Application Using WPF in C#

LINQ To SQL Examples

Understanding the Concept of Nested Classes in C#

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

Examples of Extension Methods in C#

Learning All Class Members in C#

Access Modifiers in C#

C# Root Class – Object

KeyValuePair and its Applications

IEnumerable and IEnumerator Interfaces

IEqualityComparer Interface

New Features in C# 9

Generic IList Interface and its Implementation in C#

Examples of Connected and Disconnected Approach in ADO.NET

How Data Binding Works in WPF

Leave a Reply

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