Six months ago I posted a comparison of a simple select statement in C-omega vs. ADO.Net which some people found very exciting.  Now that Linq has been officially unveiled, I figured I should update my comparison using C# 3.0 and DLinq.  Although Linq and C-omega have some significant differences, everything I said in that post about the benefits of C-omega applies equally well to Linq (in fact, I like Linq even better, primarily due to expression trees) [Update: Erik Meijer e-mailed me to say that C-omega also has indirect support for expression trees by exposing the parse tree to compiler plug-ins.]

Here is the example ADO.Net code using strongly-typed data sets:

      SqlDataAdapter da = new SqlDataAdapter(

            "SELECT * FROM Employees WHERE City= @city", nwindConn );

      SqlParameter cityParam = da.SelectCommand.Parameters.Add("@city", SqlDbType.VarChar, 80);

      cityParam.Value = city;

 

      NorthwindDataSet ds = new NorthwindDataSet();

      da.Fill(ds, ds.Employees.TableName );

 

      foreach (NorthwindDataSet.EmployeesRow dr in ds.Employees.Rows)

      {

            string name = dr.LastName;

            int id = dr.EmployeeID;

            Console.WriteLine( id + ": " + name);

      }

And here is the equivalent C# 3.0 code:

var rows = from e in db.Employees where e.City == city select e;
foreach( var e in rows )
{
   string name = e.LastName;
   int id = e.EmployeeID;
   Console.WriteLine( id + ": " + name );
}

Or even more concisely:

foreach( var e in db.Employees.Where( e => e.City == city ) )
   Console.WriteLine( e.EmployeeID + ": " + e.LastName );

There is already tons of information available about Linq (more so than there ever was for C-omega), and there are some great blogs by plenty of people more authoritative than myself (eg Matt, Dinesh, Paul, Dan, Scott, etc.), so I probably won't say much more about it myself.  But isn't it awesome that this is almost certainly going to become part of mainstream programming in a couple years?  Are we finally on the road to eliminating SQL injection attacks (not to mention all the other great benefits)?  It's advances like this that make me proud to work for Microsoft!