Posts Tagged ‘LINQ’

More on LINQ – Bane and Boon

November 2, 2005

As mentioned in previous entry, LINQ will be an enterprise developer’s dream.

I call it a boon because it will provide an easier programming model making developers more productive.

I also call it a bane because developers will use it to build richer more complex applications requiring more development time.

Before I get too philosophical, let’s do some deep diving into LINQ.

LINQ will be distributed as part of the WinFX API.
A beta 1 is currently available to developers here.

The Key Benefits of LINQ are:
· Unified querying of objects, relational, XML
· Type checking and IntelliSense for queries
· Provides SQL and XQuery capability in C# and VB

To pull this off, the following features have been added to the .NET Framework.

# Feature Example
1. Lambda expressions c => c.Name
2. Extension methods static void Dump(this object o);
3. Local variable type inference var x = 5;
4. Object initializers new Point { x = 1, y = 2 }
5. Anonymous types new { c.Name, c.Phone }
6. Query expressions from … where … select
7. Expression trees Expression<T>

I will be drilling down further over the next few days.

Programming XML with XLINQ

November 2, 2005

To illustrate the power of XLINQ, let’s see some code.

Simple XML Document:

<contacts>
  <contact>
    <name>Sandra Day O’Connor</name>
    <phone>(503) 555-7123</phone>
  </contact>
   …
</contacts>

Programming XML today

  XmlDocument doc = new XmlDocument()#160; 
                               // 1. Imperative model
  XmlElement contacts = doc.CreateElement("contacts");
                               // 2. Document centric

  foreach (Customer c in customers)
                               // 3. No integrated queries
    if (c.Country == "USA")
    {
      XmlElement e = doc.CreateElement("contact");

      XmlElement name  = doc.CreateElement("name");
      name.InnerText   = c.CompanyName;
      e.AppendChild(name);     // 4. Memory intensive

      XmlElement phone = doc.CreateElement("phone");
      phone.InnerText  = c.Phone;
      e.AppendChild(phone);

      contacts.AppendChild(e);
    }

  doc.AppendChild(contacts);

Programming XML with XLINQ.

  XElement contacts = new XElement("contacts",
                                        // 1. Declarative model
    from c in customers                 // 2. Element centric
    where c.Country == "USA"
    select new XElement("contact",
      new XElement("name", c.CompanyName), 
                                        // 3. Integrated queries
      new XElement("phone", c.Phone)    // 4. Smaller and faster
    )
  );

 

Microsoft’s LINQ will be a Productivity Boon

October 13, 2005
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