Last modified on October 26, 2016, at 22:07

LINQ

LINQ (Language Integrated Query) is a technology created by Microsoft, first introduced in version 3.0 of the .NET framework. It was designed to bridge the gap between data and programming by introducing a SQL-style language format to the .NET framework. LINQ can use either Lambda expressions or comprehension syntax. For example, given a collection of strings in C#:

string[] names = {"Alan", "Ben", "Carl", "David"};

The lambda format for a query would like like:

IEnumerable<string> query = names
  .Where  (n => n.Contains("e"))
  .Select (n => n.ToUpper());

Which would create an enumerable list containing just "BEN". The equivalent comprehension expression would look like:

IEnumerable<string> query =
  from n in names
  where n.Contains("e")
  select n.ToUpper();

An interesting point is the fact that the select statement comes last, instead of in SQL where the projection is always the first statement. This is because having the projection first would not be syntactically correct in .NET, where all variables must be declared before using them.

LINQ became especially useful for development groups who had coded in a proper object oriented (OO) fashion, utilizing generic properties of custom business objects, for example, a method with the following signature:

Public Function Departments() As Generic.IList(of Depertment)

Could suddenly be included in a Linq query, as the IList interface implements IEnumerable, thus:

Dim eCollection As IEnumerable(Of Department) = From d As Department In MyObject.Departments Where d.CompanyId = 1 Select d

Of course, when factoring in joins (equivalent to inner joins in SQL) and group joins (equivalent to outer joins in SQL) the full power of Linq becomes obvious. The reduction of code duplication is immense, and as Linq can be applied across technologies (Linq-to-SQL, Linq-To-Xml etc.) it becomes clear that the origin of the data is less significant than the data itself.