views:

298

answers:

2

I have to below 2 LINQ statements. They both return (seemingly) the same result set. Can anyone explain to me why I should use one way versus the other? Is it as simple as "You say potato, I say potato; you say tomato, I say tomato"?

Here are the two flavors of LINQ -->

1) The two lets below are to private Methods that take an ID and return the NAME.

var business = from businesse in context.tblBusinesses
               where businesse.BusinessID == businessID
               join addresse in context.tblAddresses on businesse.BusinessID equals addresse.BusinessID
               let stateName = GetStateNameByID(addresse.StateID)
               let countyName = GetCountyNameByID(addresse.CountyID)
               select new
               {
                   businesse.BusinessName,
                   businesse.ContactName,
                   businesse.EmailAddress,
                   addresse.AddressLine1,
                   addresse.AddressLine2,
                   addresse.AddressLine3,
                   addresse.CityName,
                   State = stateName,
                   addresse.ZipCode,
                   addresse.ZipPlus,
                   County = countyName
               };

2)

var query = from businesse in context.tblBusinesses
            where businesse.BusinessID == businessID
            select new
            {
                businesse.BusinessName,
                businesse.ContactName,
                businesse.EmailAddress,
                Address = from addresse in businesse.tblAddresses 
                          select new 
                          {
                              addresse.AddressLine1,
                              addresse.AddressLine2,
                              addresse.AddressLine3,
                              addresse.CityName,
                              State = addresse.StateID,
                              addresse.ZipCode,
                              addresse.ZipPlus,
                              County = addresse.tblAdminCounty
                          }
            };
+4  A: 

When you watch the sql server profiler, you see that the second one creates many queries but the first one gets all data in one query. So the first one is more efficient.

NetSide
Really, that is surprising, to me anyway, as I was leaning towards agreeing with Jon Skeet.
Refracted Paladin
Are you sure about that, @NetSide?
DanM
Yes I am sure, for each business it creates sql queries for address. I had the similar experience. if I am using LINQ, I always keep my sql profiler open :)
NetSide
+1  A: 

Are you sure they give the same result?

It looks like the first example would flatten your Address property into multiple properties, while your second example would have an Address property that itself contains properties.

But otherwise, I would say the difference between joining and an "inner select" would be a matter of personal preference. I'd probably prefer to go with a join because I'm used to writing SQL and having the word join there makes your intent obvious. But I don't see a problem with using an inner select, either.

DanM
"Are you sure they give the same result?" -- I guess when I said that I meant that the fields have the same value(ie. State=CA in both) I will have to think on it to understand if the properties make a difference in how I use them for me.
Refracted Paladin