Language Integrated Query (LINQ) is a Microsoft .NET Framework component that adds native data querying capabilities to .NET languages. Please consider using more detailed tags when appropriate, for example [linq-to-sql], [linq-to-entities] / [entity-framework], or [plinq]
This tag is for questions about linq, 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.
Please consider using more detailed tags when appropriate, for example linq-to-sql, linq-to-entities / entity-framework, or plinq.
About LINQ
All data sources can be queried using the exact same, readable and easy-to-use syntax - or rather, syntaxes, because LINQ supports two notations:
Inline LINQ or query syntax, where queries are expressed in a SQL-like language, with dialects in both C# and VB.NET.
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:
- Obtain the data source.
- Create the query.
- Execute the query.
Major Implementations:
.NET languages: C#, F#, VB.NET
Some examples:
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.
Flavors
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.
Other implementations of LINQ can be found on the Internet, such as LINQ to SharePoint, LINQ to Twitter, LINQ to CSV, LINQ to Excel, LINQ to JSON and LINQ to Google.
There are also lots of extensions for LINQ available, which add more operators to the ones .NET offers. A variety of those are open-source projects, for example MoreLINQ.
Resources
- Wikipedia article.
- LINQPad - a lightweight prototyping tool.