Overview

The LINQ acronym represents the Language Integrated Query programming model birthed by Microsoft.

It allows for full/native integration of queries in code, and gives queries first-class status in a .NET language application. It makes queries indistinguishable from standard classes, methods, control statements, or other constructions within a language allowing for full functional integration. It simplifies the overall process of querying and working with data through permitting the use of less technologies (e.g., XQuery, Web Services, and more) to achieve a goal.

Its integration reduces the code and code logic required to perform query operations on data by allowing constructions such as large control statements to instead appear as short queries. It also eliminates the need for providers in modest data operations through using its types, e.g., LINQ to Objects and LINQ to DataSet. It simplifies the general process of producing query code because no distinct separation or barrier (meaning dramatically different code) exists between the query and other members of the application. It truly functions as an integrated element.

Beyond its syntactic sugar, LINQ also offers incredible flexibility. It partners with any entity including, but not limited to SQL Server, MySQL, and NoSQL. It couples this flexibility with utilizing a single technology and simple, dedicated tool regardless of the entity. This frees development from many limitations or hindrances resulting from the entity used.

LINQ uses SQL-like query expressions to perform common query operations such as filtering, grouping, and ordering. LINQ queries retrieve from a data source, and the applications in which the queries appear all view the source as either
IEnumberable<T> or IQueryable<T> collections regardless of type. LINQ, unlike embedded SQL, can invoke native structures and constructions. It also has access to a wider range of data due to its mechanisms interacting with just about every possible source as easily as native statements. Embedded SQL only accesses databases specific to its implementation.

This guide focuses on LINQ applied to C# applications, but LINQ integrates with almost any .NET language. It assumes knowledge of query languages, databases, C#, and any other related technology. The example below forms a LINQ “Hi Universe” program. It utilizes a LINQ expression to retrieve values:

using System;
using System.Linq;
				
class HiUniverseProgram
{
	static void Main()
	{
		string[] terms = {"Hi", "supercalifragilisticexpialidocious", "awesomeness",
"universe"};
		//Retrieve words less than 9 characters
		var shortTerms = from term in terms
					 where term.Length <= 9
					 select term;

		//Print
		foreach (var term in shortTerms)
		{
		Console.WriteLine(term);
		}
		Console.ReadLine();
	}
}

The same application in standard C# (without the use of a LINQ expression) requires the use of control statements to retrieve and filter appropriate string values, making the code much more verbose, and time-consuming to interpret.