Microsoft’s LINQ will be a Productivity Boon

There is much to be said about Microsoft’s new LINQ technology.
LINQ stands for Language Integrated Query.
 
I will be blogging more about the details in the future.
 
However, the big advantage for most developers will be:
  1. conciseness of the code (less code to write, less code to test)
  2. compile time checking
 
Here’s an example of data access today versus data access with LINQ.
 
It’s a case of 10 lines versus 5 lines.
 
Accessing Data Today
 
Line #
 
1.  SqlConnection c = new SqlConnection(…);
 
2.  c.Open();
 
3.  SqlCommand cmd = new SqlCommand(
       @"SELECT c.Name, c.Phone    // 1. Queries in quotes
            FROM Customers c
            WHERE c.City = @p0");  // 2. Loosely bound arguments
 
4.  cmd.Parameters.AddWithValue("@p0", "London");
 
5.  DataReader dr = c.Execute(cmd);
 
6.  while (dr.Read()) 
    {
7.    string name = dr.GetString(0);
                                 // 3. Loosely typed result sets
8.    string phone = dr.GetString(1);

9.    DateTime date = dr.GetDateTime(2);

                                 // 4. No compile time checks 
    }
 
10. dr.Close();
 
 
Accessing Data with LINQ
 
Line #
 
1.  public class Customer { … }  // 1. Classes describe data
 
2.  public class Northwind: DataContext
     {
3.    public Table<Customer> Customers;
                             // 2. Tables are like collections
       …
     }
4.  Northwind db = new Northwind(…);
                            // 3. Strongly typed connection
 
5.  var contacts =
        from c in db.Customers
        where c.City == "London"
                            // 4. Integrated query syntax
        select new { c.Name, c.Phone };
                            // 5. Strongly typed results
 
Advertisements

Tags:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: