开发者

When to use JOIN and when not to in LINQ to entities

I am new to Linq and I have seen that if there are multiple entities, some use the multiple FROM syntax like this:

from h in db.Hubs   
from ch in h.CityHubs where ch.Cities.CityID == 1 
select

and some use the explicity join syntax.

from h in db.Hubs
join ch in da.CityHubs on h.CityId equals ch.CityId
select

If I am using Linq to entities, which one should I use? If I were to use L开发者_运维百科inq to objects, which one should I use?


As a rule, in Entity Framework, if you have a proper model and properly set up navigation properties for foreign keys, you should almost never use join - instead you access your navigational property directly and EF will generate the necessary join in the SQL code for you. I recommend taking a look at @Craig Stuntz's blogpost regarding this issue. Regarding Linq-to-objects, however, it depends on the particular query you are writing.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜