Tip:
Highlight text to annotate it
X
LINQ
Language Integrated Query or LINQ (pronounced link) is a C# extension that integrates data inquiries and to C# code. LINQ avoids a lot of
loops and repeated code saving time and streamlining the code. LINQ can query many types of data from databases to XML to Web
services.
Code must reference the System.Xm;.Linq namespace:
using System.Xml.Linq;
LINQ is too expensive to go into great depth but it is good to be familiar with some of the most common methods. LINQ to XML is a set of
classes used to enable LINQ for XML data. There are also constructors called functional constructors that build XML documents. LINQ to
XML also works with incomplete XML documents which XML.DOM cannot although there are some limits.
A sample LINQ query might look like this:
var queryResults = from n in names where n.StartsWith("S")
select n;
The query first declares the variable intended to contain the results. The from clause denotes the data to be queried (must be an array or
collection). The where clause is a restriction operator and sets conditions for the query. Select determines what is shown in the results.
The foreach statement can be used to print results:
WriteLine("Names beginning with S:"); foreach (var item in queryResults) {WriteLine(item);
}