Friday, January 16, 2009

SubSonic versus LinqToSQL: Simple Select

I have used SubSonic for years, and quite frankly was avoiding LinqToSQL because I didn't think that I would want to use it. After playing around with LinqToSQL a bit (forced by using ASP.NET Dynamic Data), it's not too bad. I thought that I would give a few quick comparisons for those who are also using SubSonic and have not drawn a correlation yet on how to achieve similar results while using LinqToSQL. Both are looking at the Northwind database, btw.

If you are looking for other posts in this series, here is the list:

  • SubSonic versus LinqToSQL: Simple Select

  • SubSonic versus LinqToSQL: Simple Insert

  • SubSonic versus LinqToSQL: Simple Update

  • SubSonic versus LinqToSQL: Simple Delete


  • Here is a Simple Select command using SubSonic:
    using SubSonic;
    using MyCustomDAL;

    ...

    protected void GetProduct(ProductID)
    {
    Product p = new Product(int ProductID)
    txtProductName = p.ProductName;
    txtUnitPrice = p.UnitPrice;
    }

    And here is the LinqToSQL equivalent:
    using System.Linq;

    ...

    protected void GetProduct(int ProductID)
    {
    NorthwindDataContext db = new NorthwindDataContext();

    var products = from p in db.Products
    where p.ID == ProductID
    select p;

    foreach (var product in products)
    {
    txtProductName = product.ProductName;
    txtUnitPrice = product.UnitPrice;
    }
    }

    As you can see, there are similarities between the two. I hope that this will encourage hardcore SubSonic users who have blown off LinqToSQL like I did to at least investigate LinqToSQL since I think it is not going away. I will still be using SubSonic where I can, but if I have a requirement of "All Microsoft" then I will be using LinqToSQL.