Living in the Tech Avalanche Generation

A practitioner’s introspective on technology

LinqPad for the Entity Framework.

I recently purchased the auto completion addon which is a neat tool for LinqPad. Its pretty cheap and certainly aided in my using LINQ Pad as a scratch pad ala Query Analyzer. For example when dealing with my own Repository and Specification libraries for Entity Framework, I can write and fine tune using scratch pad code along with my libraries directly in LINQ Pad. The benefit is I can verify my queries without any debugging in VS.Net. To use my own libraries that contain the Entity Framework code I can reference those libraries as per below:

refs_linqpad

and import their namespaces into LinqPad like so:

namespaces_linqpad

No I can write code such as:

thepad

If you download my library, you can run this code after setting up as per the above.

NorthwindModel.NorthwindEntities context =
    new NorthwindModel.NorthwindEntities(
  @”Provider=
    System.Data.SqlClient;
  Provider Connection String=
    ‘Data Source=boomer;
    Initial Catalog=Northwind;
    Integrated Security=True;
    Connection Timeout=5;
    MultipleActiveResultSets=true;’;
      Metadata=
    res://*/”);

var german_cust_spec =
    new Specification<NorthwindModel.Customer>
    (c => c.Country == “Germany”);
var italian_cust_spec =
    new Specification<NorthwindModel.Customer>
    (c => c.Country == “Italy”);

var spec = italian_cust_spec | german_cust_spec;

var query = from cust in context.Customers
            .Where(spec.EvalPredicate)
            orderby cust.Country
            select cust;

query.Dump();

I absolutely love the fact that I have a tactile query analyzer(ish) IDE to test my Entity Framework queries prior to writing them in VS.Net and further to that I don’t miss out on using my custom libraries that contain classes such as the Specification<T> or  EntitiesRepository<E, C> as demonstrated below.

NorthwindModel.NorthwindEntities context =
    new NorthwindModel.NorthwindEntities(
  @”Provider=
    System.Data.SqlClient;
      Provider Connection String=
    ‘Data Source=boomer; Initial Catalog=Northwind;
    Integrated Security=True; Connection Timeout=5;
    MultipleActiveResultSets=true;’;
      Metadata=
    res://*/”);

var german_cust_spec =
    new Specification<NorthwindModel.Customer>(c => c.Country == “Germany”);
var italian_cust_spec =
    new Specification<NorthwindModel.Customer>(c => c.Country == “Italy”);

var spec = italian_cust_spec | german_cust_spec;

EntitiesRepository<NorthwindModel.Customer, NorthwindModel.NorthwindEntities>
    repos = new
    EntitiesRepository<NorthwindModel.Customer, NorthwindModel.NorthwindEntities>
    (context);

var repos_query = from cust in repos.All(spec)
        select cust;

query.Dump();

If your a LinqPad user you might want to consider buying the LINQPAD auto completion add-on which I get great value out of. I would however love Joseph to make LINQPAD more Entity Framework friendly and provide us the SQL output created by the Entity Framework Provider as it does for LINQ To SQL.

Thanks to Stefan for demonstrating how to use LINQPAD with the Entity Framework in the first instance. Give it a try.

Share/Save/Bookmark

3 Comments so far

  1. [...] LinqPad for the Entity Framework (Simon Segal via Shawn Wildermuth) [...]

  2. Craig Lebowitz January 29th, 2009 6:48 am

    Excellent post, it helped me get up and running with the ‘pad and entity framework.

    [Reply]

  3. [...] that what looks to make sense in your LINQ C#/VB code is not making sense as SQL. I recently described a way of working with LinqPad that will allow you to write queries in both the Entity Framework and [...]

Leave a reply

Creative Commons Attribution-ShareAlike 2.5 Australia
Creative Commons Attribution-ShareAlike 2.5 Australia