Entity Framework Query for inner join

后端 未结 3 1556
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-12 19:22

What would be the query for:

select s.* from Service s 
inner join ServiceAssignment sa on sa.ServiceId = s.Id
where  sa.LocationId = 1

in

3条回答
  •  一向
    一向 (楼主)
    2020-12-12 20:21

    In case anyone's interested in the Method syntax, if you have a navigation property, it's way easy:

    db.Services.Where(s=>s.ServiceAssignment.LocationId == 1);
    

    If you don't, unless there's some Join() override I'm unaware of, I think it looks pretty gnarly (and I'm a Method syntax purist):

    db.Services.Join(db.ServiceAssignments, 
         s => s.Id,
         sa => sa.ServiceId, 
         (s, sa) => new {service = s, asgnmt = sa})
    .Where(ssa => ssa.asgnmt.LocationId == 1)
    .Select(ssa => ssa.service);
    

提交回复
热议问题