Monday, January 24, 2011

What Is LINQ? And How to install it?

LINQ is a combination of namespaces and C# 3.0 language enhancements. Through the very clever use of generics and other powerful new features of .NET 2.0 and using some functional programming techniques (like those natively available in F#), LINQ provides a high-level abstraction of virtually any data and emulates the query operations of the relational model. The LINQ Project seems to be just the beginning of many other future dramatic enhancements to .NET and .NET languages.

LINQ has three major components:
• LINQ to Objects
• LINQ to ADO.NET, which includes
o LINQ to DataSet (originally called LINQ over DataSet)
o LINQ to Entities
o LINQ to SQL (originally called DLinq)
• LINQ to XML (originally called XLinq)
LINQ to Objects deals with in-memory data. Any class that implements the IEnumerable interface (in the System.Collections.Generic namespace) can be queried with SQO. 

LINQ to ADO.NET deals with data from external sources, basically anything ADO.NET can connect to. Any class that implements IEnumerable or IQueryable (in the System.Query namespace) can be queried with SQO.


LINQ to XML is a comprehensive API for in-memory XML programming. Like the rest of LINQ, it includes SQO, and it can also be used in concert with LINQ to ADO.NET, but its primary purpose is to unify and simplify the kinds of things that disparate XML tools, like XQuery, XPath, and XSLT, are typically used to do. In this chapter we’ll preview LINQ to SQL and LINQ to DataSet, since they’re most closely related to the C# database programming covered in this.

Installing LINQ
 

installing LINQ doesn’t replace any .NET 2.0 assemblies, but it does change our VCSE development environment, adding some new project types that support LINQ and use the C# 3.0 compiler
The May 2006 LINQ CTP (Community Technology Preview) can be downloaded from the LINQ Project home page, http://msdn.microsoft.com/data/ref/linq/. Go there and click Microsoft Visual Studio Code Name "Orcas" - LINQ CTP (May 2006), which will take you to the download page. It’s small enough to just run the download if you have a reasonably fast Internet connection, but we save it to c:\bcs2005db\install.11
 

To install LINQ:
 

1. Run LINQ Preview (May 2006).msi, which starts the LINQ installation process. When the Welcome window appears (see Figure), click Next.

2. When the License Agreement window appears (see Figure), click the I Agree radio button and when the Next button is enabled, click it. 















LINQ installation Welcome window



LINQ License Agreement window


3. When the Update C# Language Service for LINQ window appears (see Figure), click the Update C# Language Service radio button and click Next.

4. When the Confirm Installation window appears (see Figure), click Next.

5. A progress window appears (see Figure). When the Next button is enabled, click it.



Update C# Language Service for LINQ window



LINQ Confirm Installation window

6. When the Installation Complete window appears (see Figure 18-6), click Close. LINQ is now installed, and you’ll find a lot of useful things in C:\Program Files\LINQ Preview. (We recommend you look at ReadMe for C#.htm.)
 





7. Open VCSE and create a new project. You should see the four new templates for LINQ shown in Figure  Select LINQ Console Application, change the project name to Chapter18, and click OK.



8. A message box will alert you to your use of an unsupported version of C# 3.0 (see Figure). Don’t worry, it works well enough for this chapter (in fact, it works quite stably). Click OK.




9. In Solution Explorer, expand the References node. Note the four new assemblies (System.Data.DLinq, System.Data.Extensions, System.Query, and System.Xml.XLinq) VCSE automatically provides (see Figure ).

10. Double-click Program.cs. Note the three new namespaces (System.Query, System.Xml.XLinq, and System.Data.DLinq) VCSE automatically provides using directives for (see Figure 18-10). Save the solution. We’re ready to do some LINQ database programming. 




Using LINQ to SQL

LINQ to SQL is a facility for managing and accessing relational data as objects. It’s logically similar to ADO.NET in some ways but views data from a more abstract perspective that simplifies many operations. It connects to a database, converts LINQ constructs into SQL, submits the SQL, transforms results into objects, and can even track changes and automatically request database updates.
A simple LINQ query requires three things:
·         An entity class
·         A data context
·         A LINQ query

Coding a Simple LINQ to SQL Query

use LINQ to SQL to retrieve all customers from the Northwind Customers table.
1. Rename the Chapter18 project in the Chapter18 solution to LinqToSql, then rename Program.cs to LinqToSql.cs. Replace the code in LinqToSql.cs with the code in Listing 18-1.
LinqToSql.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Query;
using System.Xml.XLinq;
using System.Data.DLinq;
 
namespace Chapter18
{
   [Table]
   public class Customers
   {
      [Column(Id=true)]
      public string customerId;
      [Column]
      public string companyName;
      [Column]
      public string city;
      [Column]
      public string country;
   }
 
   class Linq To Sql
   {
      static void Main(string[] args)
      {
         // connection string
         string connString = @"
            server = .\sqlexpress;
            integrated security = true;
            database = northwind
         ";
 
         // create data context 
         DataContext db = new DataContext(connString);
 
         // create typed table 
         Table customers = db.GetTable();
 
         // query database
         var custs =
            from c in customers
            select
               c
         ;
 
         // display customers
         foreach (var c  in custs)
            Console.WriteLine(
               "{0} {1} {2} {3}",
               c.customerId,
               c.companyName,
               c.city,
               c.country
            );
      }
   }
}
2. Run the program with Ctrl+F5 and you should see results as in Figure  (which displays the last ten rows).














No comments:

Post a Comment