开发者

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

开发者 https://www.devze.com 2023-03-01 22:06 出处:网络
I am new to Linq and I have seen that if there are multiple entities, some use the multiple FROM syntax like this:

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

精彩评论

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

关注公众号