开发者

LINQ, Should I JOIN or use nested SELECT NEW's

开发者 https://www.devze.com 2022-12-18 07:52 出处:网络
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 开发者_

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
                          }
            };


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消