LINQ

From Conservapedia
This is an old revision of this page, as edited by MattS (Talk | contribs) at 22:31, May 31, 2009. It may differ significantly from current revision.

Jump to: navigation, search

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.