Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

17.5. The Bottom Line

Perform simple LINQ queries.

A LINQ query starts with the structure From variable In collection, Where variable is a variable name and collection is any collection that implements the IEnumerable interface (such as an array, a typed collection, or any method that returns a collection of items). The second mandatory part of the query is the Select part, which determines the properties of the variable we want in the output. Quite often we select the same variable that we specify in the From keyword. In most cases, we apply a filtering expression with the Where keyword. Here's a typical LINQ query that selects FileNames from a specific folder:

Dim files = _
        From file In _
          Where file.EndsWith("doc")
        Select file

Master It

Write a LINQ query that calculates the sum of the squares of the values in an array.

Create and process XML files with LINQ to XML.

LINQ to SQL allows you to create XML documents with the XElement and XAttribute classes. You simply create a new XElement object for each element in your document, and a new XAttribute object for each attribute in the current element. Alternatively, you can simply insert XML code in your VB code. To create an XML document dynamically, you can insert embedded expressions that will be evaluated by the compiler and replaced with their results.

Master It

How would you create an HTML document with the FileNames in a specific folder?

Process relational data with LINQ to SQL.

LINQ to SQL allows you to query relational data from a database. To access the database, you must first create a DataContext object. Then you can call this object's GetTable method to retrieve a table's rows, or the ExecuteQuery method to retrieve selected rows from one or more tables with an SQL query. The result is stored in a class designed specifically for the data you're retrieving via the DataContext object.

Master It

Explain the attributes you must use in designing a class for storing a table.


You are currently reading a PREVIEW of this book.


Get instant access to over $1 million worth of books and videos.


Start a Free 10-Day Trial

  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint