LINQ Flavors


LINQ is a technology that covers many data domains. Some of these domains are included in those “LINQ Flavors” that Microsoft provides as part of the .NET 3.5 Framework, as shown in Figure 1-1.

image from book
Figure 1-1: LINQ implementations provided by Microsoft within .NET 3.5

Each of these implementations is defined through a set of extension methods that implement the operators needed by LINQ to run over a particular data domain. The access to these features is controlled by the imported namespaces.

More Info 

Namespaces are imported in the current scope of a program through the keywords using in C# and Imports in Visual Basic.

LINQ to Objects

LINQ to Objects has the goal of manipulating collections of objects, which can be related to each other to form a hierarchy or a graph. From a certain point of view, LINQ to Objects is the default implementation used by a LINQ query. LINQ to Objects is enabled including the System.Linq namespace.

More Info 

The base concepts of LINQ are explained using LINQ to Objects as a reference implementation in Chapter 4, “LINQ Syntax Fundamentals.”

It would be a mistake to think that LINQ to Objects queries are limited to collections of user-generated data. You can see why this is not true by analyzing Listing 1-11, which shows you a LINQ query over information extracted from the file system. The list of all files in a given directory is read in memory before being filtered by the LINQ query.

Listing 1-11: LINQ query that gets temporary files greater than 10,000 bytes, ordered by size

image from book
  string tempPath = Path.GetTempPath(); DirectoryInfo dirInfo = new DirectoryInfo( tempPath ); var query =     from    f in dirInfo.GetFiles()     where   f.Length > 10000     orderby f.Length descending     select  f; 
image from book

LINQ to ADO.NET

LINQ to ADO.NET includes different LINQ implementations that share the need to manipulate relational data. It includes other technologies that are specific to each particular persistence layer:

  • LINQ to SQL   Handles the mapping between custom types in C# and the physical table schema.

  • LINQ to Entities   Is in many ways similar to LINQ to SQL. However, instead of using the physical database as a persistence layer, it uses a conceptual Entity Data Model (EDM). The result is an abstraction layer that is independent from the physical data layer.

  • LINQ to DataSet   Makes it possible to query a DataSet using LINQ.

LINQ to SQL and LINQ to Entities have similarities because they both access information stored in a relational database and operate on object entities that represent external data in memory. The main difference is that they operate at a different level of abstraction. While LINQ to SQL is tied to the physical database structure, LINQ to Entities operates over a conceptual model (business entities) that might be far from the physical structure (database tables).

The reason for these different options for accessing relational data through LINQ is that different models for database access are in use today. Some organizations implement all access through stored procedures, including any kind of database query, without using dynamic queries. Many others use stored procedures to insert, update, or delete data and dynamically built SELECT statements to query data. Some see the database as a simple object persistence layer, while others put some business logic into the database using triggers, stored procedures, or both. LINQ tries to offer help and improvements in database access without forcing everyone to adopt a single comprehensive model.

More Info 

The use of any LINQ to ADO.NET feature depends on the inclusion of particular namespaces in the scope. LINQ to ADO.NET implementations and similar details are investigated in Chapter 5.

LINQ to XML

LINQ to XML offers a slightly different syntax that operates on XML data, allowing query and data manipulation. A particular type of support for LINQ to XML is offered by Visual Basic 9.0, which includes XML literals in the language. This enhanced support simplifies the code necessary to manipulate XML data. In fact, you can write such a query in Visual Basic 9.0:

 Dim book = _     <Book Title="Introducing LINQ">         <%= From person In team _             Where person.Role = "Author" _             Select <Author><%= person.Name %></Author> %>     </Book>

This query corresponds to the following C# 3.0 syntax:

 dim book =     new XElement( "Book",         new XAttribute( "Title", "Introducing LINQ" ),         from   person in team         where  person.Role == "Author"         select new XElement( "Author", person.Name ) );

More Info 

You can find more information about XML support in Visual Basic in Chapter 3. The general LINQ to XML implementation is discussed in Chapter 6.




Introducing Microsoft LINQ
Introducing MicrosoftВ® LINQ
ISBN: 0735623910
EAN: 2147483647
Year: 2007
Pages: 78

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net