LINQ (Language Integrated Query) is a feature in C# that provides a way to query and manipulate data from different sources such as collections, databases, XML or objects. It integrates query capabilities directly into the C# language using a set of query operators and extension methods.

LINQ provides a declarative, SQL-like syntax for filtering, sorting, grouping and transforming data.
Example:
using System;
using System.Linq;
class Program {
static void Main() {
int[] numbers = { 2, 5, 7, 8, 10 };
// LINQ query to get numbers greater than 5 and double them
var result = numbers.Where(n => n > 5).Select(n => n * 2);
Console.WriteLine("Result of LINQ query:");
foreach (var n in result) {
Console.WriteLine(n);
}
}
}
Output
Result of LINQ query: 14 16 20
LINQ Query flow
- Data Source: It is the collection of elements on which the query will run. In code, int[] nums = { 2, 5, 7, 8, 10 } is the data source.
- Filtering: It is the process of selecting elements based on a condition. In code, Where(n => n > 5) filters numbers greater than 5.
- Projection: It transforms each element into a new form. In code, Select(n => n * 2) doubles each filtered number.
- Execution: The query actually runs when iterated. In code: The foreach loop executes the query and prints results.
Note: LINQ supports both query syntax and method (lambda) syntax, making code readable, concise and consistent across data sources.
Architecture of LINQ
LINQ follows a three-layered architecture:

- Top Layer (Language Extensions): Integrates query capabilities directly into C# and VB.NET using query syntax and lambda expressions.
- Middle Layer (LINQ Providers): Translates queries into the format understood by different data sources (e.g., LINQ to Objects, LINQ to SQL, LINQ to XML).
- Bottom Layer (Data Sources): The actual data collections, typically objects implementing IEnumerable<T> or IQueryable<T>, such as arrays, lists, databases or XML documents.
Types of LINQ
LINQ in C# is categorized into several types based on the data source it works with. Some of them are:
1. LINQ to Objects
- Works with in-memory collections like arrays, List<T>, Dictionary<TKey, TValue>, etc.
- Use Case: Query, filter, sort and transform data directly in collections without writing loops.
2. LINQ to SQL
- Works with SQL Server database.
- Use Case: Converts LINQ queries into SQL commands and executes them on the database. Useful for strongly typed, compile-time checked queries.
3. LINQ to XML
- Works with XML documents (XElement, XDocument).
- Use Case: Simplifies reading, querying and updating XML data with LINQ syntax instead of manual XML parsing.
4. LINQ to Entities
- Works with Entity Framework models connected to relational databases.
- Use Case: Enables database queries at an object level using entities instead of raw SQL.
5. LINQ to DataSet
- Works with DataSet and DataTable objects in ADO.NET.
- Use Case: Useful in disconnected environments where data is fetched into a DataSet and then LINQ is applied for filtering or transformation.
Benefits of using LINQ
- Simplifies querying of arrays, collections, XML, SQL and entities
- Reduces code length and improves readability compared to loops or delegates
- Provides compile-time type checking and IntelliSense support
- Supports filtering, sorting, grouping and data transformations
- Enables reusable queries across different data sources without learning new query languages
- Integrates seamlessly with C# for easier debugging