Writing Entity Framework queries using LINQPad

I've been doing a bit of query tuning lately and have been introduced to LINQPad which makes writing and tuning LINQ queries easy.

In this example I use the good old AdventureWorks database. I have an entity framework data access method that returns Customers for an email address

/// <summary>
/// Gets the customers with the given email address
/// </summary>
/// <param name="emailAddress">The email address.</param>
/// <returns>A list of customers</returns>
public IList<Customer> GetCustomers(string emailAddress)
{
     using (var connection = new AdventureWorksLT2008Entities())
     {
         return connection.Customer.Where(c => c.EmailAddress == emailAddress).ToList();
     }
}

A fast way to figure out the sql that this query is executing is to fire up LINQPad and point it at the assembly with the Entity Framework data model (EDMX) file in it. If we add a connection, we can choose to add a typed data context from our own assembly

Once we point it at our assembly, LINQ pad detects that we have an EF data context and fills in the rest of the details from the default connection string used by the EDMX (in the assembly config file, which by default is copied to the output directory).

 

We now get a tree view of the various entity collections in our data context. We can copy our query into the query window, make a few tweaks like in-lining parameters with example values and execute our query against the database. LINQPad displays the results in a nice table view by default.

If we click on the SQL tab we can see the SQL that the EF engine has generated, from where we can copy it into SSMS or a similar tool to interrogate the execution plan and determine if it will perform well. This also allows you to play around with different ways of writing LINQ queries to find the method that generates the best SQL while returning the data you need.

It's a bit of a trivial example but shows a real world approach to query development using Entity Framework. The ORM may abstract you from writing SQL but the way your queries are executed by the SQL engine is as important as ever!

Tags: , , , ,

Comments

trackback
DotNetShoutout
3/18/2011 3:07:58 AM Permalink

Writing Entity Framework queries using LINQPad

Thank you for submitting this cool story - Trackback from DotNetShoutout

trackback
DotNetKicks.com
3/18/2011 3:09:15 AM Permalink

Writing Entity Framework queries using LINQPad

You've been kicked (a good thing) - Trackback from DotNetKicks.com

patent attorney
patent attorney India
11/5/2011 3:57:39 PM Permalink

We provide a diverse range of services, including prior art research, patent alert services,

Printing Companies
Printing Companies India
11/5/2011 4:04:06 PM Permalink

Our major products are Car Bumper Stickers, Custom Size and Shape Stickers Printing, Packing Boxes Printing, Vinyl Banners Printing we are an established digital and print media marketing and online advertisement company. We have spent a considerable amount of time on learning and improving the strategies getting most out-put out of digital and offset printing techniques and website optimization.

http://it.casinocoverage.com
http://it.casinocoverage.com United States
11/15/2011 7:21:29 AM Permalink

I like your blog on the internet because it’s written wonderfully and great as compare to another blogs on the internet. Thanks you for your blog posted greatly on the internet.

Recensione di casino bodog
Recensione di casino bodog United States
11/15/2011 7:22:33 AM Permalink

I like your blog on the internet because it’s written wonderfully and great as compare to another blogs on the internet. Thanks you for your blog posted greatly on the internet.

Top Listings
Top Listings United States
12/4/2011 8:18:58 AM Permalink

I am glad that you shared this useful info with us. Please keep us informed like this. Thanks for sharing….

foam mattress
foam mattress United States
12/8/2011 6:11:11 AM Permalink

We commend every organization that is working in Haiti to alleviate suffering and offer essential services, but we’d like to suggest a few groups that both receive high ratings from Charity Navigator and take a secular approach to their charitable work.

ab circle pro reviews
ab circle pro reviews United States
12/14/2011 7:31:25 AM Permalink

Linqpad sounds new to me. But it's worthy a try. Every software must have its benefits. Thanks!

indian wedding dj
indian wedding dj United States
12/15/2011 7:07:52 AM Permalink

I love your introduction about LinqPad. It sure does explain a lot about the software. People will get encouraged on trying this one out. Many thanks to you.

Aquaponics 4 You
Aquaponics 4 You United States
1/11/2012 4:23:17 AM Permalink

This is new to me. I think I should try this one. Thanks for sharing.

Agriculture Equipments
Agriculture Equipments United States
1/24/2012 9:50:02 AM Permalink

This one of the highly realistic and informatics blog I have ever read. Every bit of information that I have read seems very authentic and genuine that has high volume of visitors.

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading