我必須低於2 LINQ語句。他們都返回(看起來)相同的結果集。任何人都可以向我解釋爲什麼我應該用一種方式與另一種方式?它是否如"You say potato, I say potato; you say tomato, I say tomato"那麼簡單?LINQ,我應該加入還是使用嵌套SELECT NEW的
下面是LINQ的兩種口味 - >
1)兩個lets
下面是受到充分的ID,並返回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
}
};
「你確定他們給出了相同的結果嗎?」 - 我想當我說我的意思是說這些字段具有相同的值(即兩者中的State = CA)時,我將不得不考慮這些屬性是否會對我如何使用它們產生影響。 – 2010-01-29 21:57:03