An Overview of LINQ

LINQ is an acronym for Language Integrated Query. It is a component introduced as a framework component with .NET Framework 3.5 that adds SQL-like querying capabilities to .NET objects. Specifically, it extends the core .NET languages (Visual Basic and C#) and the runtime to try to erase the object-to-database-entity barrier. Both Visual Basic and C# support new query operators that operate over objects similar to the way SQL operates over tables in a database.

For example, you could query for all approved invoice objects like this.

var approved =
    from invoice in invoices
    where (invoice.Approved) == true
    select invoice;

foreach (Invoice invoice in approved)
{
    // do some work here
}

Runtime support is introduced for physically translating objects and methods to and from their database equivalents (primarily through the use of code attributes, as you see in a moment). This is a simple example of a class method mapped to a SQL Server stored procedure.

[Function(Name="HR.uspDeleteEmployee")]
public int uspDeleteEmployee([Parameter(Name="EmployeeID", DbType="Int")]
                              System.Nullable<int> employeeID)
{
   IExecuteResult result = this.ExecuteMethodCall(this,
            ((MethodInfo)(MethodInfo.GetCurrentMethod())), employeeID);
   return ((int)(result.ReturnValue));
}

LINQ comes in several flavors, each targeted at a specific mapping problem:

Image LINQ to SQL—This enables you to map objects to database entities.

Image LINQ to XML—This enables you to query XML documents and map objects to XML document elements.

Image LINQ to Objects—This specifically refers to the inclusion of .NET language syntax that enables queries to be written over collections of objects (as in our previous example with the approved invoices).

You need to be aware of one important fact regarding LINQ to SQL: although Microsoft fully supports the technology, it has been deprecated in favor of the Entity Framework. Some developers might still prefer to use LINQ to SQL because it is a lighter weight and simpler object-relational mapping (ORM) to implement.


Note

In 2007, Microsoft released a technology preview of a version of LINQ designed specifically for parallel execution. This technology, called PLINQ (for Parallel LINQ), has now been officially released. In essence, PLINQ builds on the LINQ concepts by accepting any LINQ to XML or LINQ to Objects query and executes those queries by optimizing for multiple CPU or multiple core scenarios.


If you are interested more in PLINQ or parallel programming in general, the Parallel Computing Center on MSDN is a great start: http://msdn.microsoft.com/en-us/concurrency/default.aspx.

LINQ is a fairly broad and deep set of technology pieces, and covering even one in depth is beyond the scope of this book. We do, however, dig into the primary Visual Studio tool used when writing LINQ to SQL applications: the O/R designer.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset