Difference between revisions of "LINQ"

From Conservapedia
Jump to: navigation, search
m (cat)
Line 1: Line 1:
 
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 (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#:
(More detail coming)
+
<code><pre>
 +
string[] names = {"Alan", "Ben", "Carl", "David"};
 +
</pre></code>
 +
The lambda format for a query would like like:
 +
<code><pre>
 +
IEnumerable<string> query = names
 +
  .Where  (n => n.Contains("e"))
 +
  .Select (n => n.ToUpper());
 +
</pre></code>
 +
Which would create an enumerable list containing just "BEN". The equivalent comprehension expression would look like:
 +
<code><pre>
 +
IEnumerable<string> query =
 +
  from n in names
 +
  where n.Contains("e")
 +
  select n.ToUpper();
 +
</pre></code>
 +
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.
  
 
[[Category:Computers]]
 
[[Category:Computers]]

Revision as of 22:31, May 31, 2009

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.