Language Integrated Query (LINQ) is a Microsoft .NET Framework component that adds native data querying capabilities to .NET languages.
linq is a .NET-based DSL (Domain Specific Language), introduced in .net-3.5, for querying data sources such as databases, XML files or in-memory object lists. All these data sources can be queried using the exact same, readable and easy-to-use syntax - or rather, syntaxes, because LINQ supports two notations:
Fluent LINQ or query operators, where queries are expressed as lambda expressions and can be linked (LINQed?) using a fluent syntax.
All LINQ query operations consist of three distinct actions: 1. Obtain the data source. 2. Create the query. 3. Execute the query.
.NET languages (C#, F#, VB.NET)
Fluent syntax (C#)
var result = dbContext.Products .Where(p => p.Category.Name == "Toys" && p.Price >= 2.50) .Select(p => p.Name);
Query syntax (C#)
var result = from product in dbContext.Products where product.Category.Name == "Toys" where product.Price >= 2.50 select product.Name;
Query syntax (VB.NET)
Dim result = From product in dbContext.Products _ Where product.Category.Name = "Toys" _ Where product.Price >= 2.50 _ Select product.Name
This query would return the name of all products in the "Toys" category with a price greater than or equal to 2.50.
LINQ comes in many flavors, the most notable are
- LINQ to Objects - For querying collections of POCO (Plain old CLR objects)
- LINQ to SQL - For querying SQL databases
- LINQ to Entities - For querying SQL databases through Entity Framework
- LINQ to XML - For querying XML documents
- PLINQ - for querying in parallel.
LINQ has many extensions implemented online and a variety of extension open-source projects like MORELINQ which adds more operators to the .NET operators, and many others.