LINQ, Data access of the future really rocks
The past two days where really packed with lots of new stuff. I must say I was a little bit disappointed at first with the keynote of Bill and Jim. I found that many of the demo’s they came up with where more or less a repeat of the stuff they did in 2003. Lots of cool demo’s on Avalon (sorry, I now must say WCF) and your usual 3D animated user interfaces. Yesterday we got to pick up the Goods as they keep calling the PDC DVD sets. Actually it appears that there are a lot of GB’s on the DVD’s but only one DVD contains stuff that is new.
They demonstrated this new stuff they call LINQ witch is really a cool way of working with data in your application. It is an extension on the .Net platform so all languages can now integrate set based query langue constructs within their normal applications. This stuff is really cool and will provide a real productivity gain in terms of writing data intensive code. Have a look on how this can change the way you write your code:
Below you see your average way of working with ADO
SqlConnection c = new SqlConnection(…);
c.Open();
SqlCommand cmd = new SqlCommand( @”SELECT c.Name, c.Phone FROM Customers c WHERE c.City = @p0″ );
cmd.Parameters.AddWithValue(“@po”, “London”);
DataReader dr = c.Execute(cmd);
while (dr.Read()) {
string name = dr.GetString(0);
string phone = dr.GetString(1);
DateTime date = dr.GetDateTime(2);
}
dr.Close();
Now the same results with the use of DLinq (This is LINQ for relation database access, they also have XLinq wich is especially for XML access and you can combine them troughout your code !)
public class Customer
{
public int Id;
public string Name;
public string Phone;
…
}
Table<Customer> customers = db.Customers;
var contacts = from c in customers
where c.City == “London”
select new { c.Name, c.Phone };
Now I have all my contacts with this DLinq expression and I can iterate over the contacts….. now that is power to the developer 🙂
In 2004 I was disappointed that Objectspaces would not make it to the final product, but now I am glad it didn’t this is way better and more powerful!