ProgramingTip

내부 조인에 대한 Entity Framework 쿼리

bestdevel 2020. 12. 3. 08:18
반응형

내부 조인에 대한 Entity Framework 쿼리


다음에 대한 쿼리는 무엇입니까?

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

널 프레임 워크에서?

이것이 내가 쓴 것입니다.

 var serv = (from s in db.Services
                join sl in Location on s.id equals sl.id
                where sl.id = s.id
                select s).ToList();

그러나 그것은 잘못되었습니다. 누군가 나를 길로 인도 할 수 있습니까?


from s in db.Services
join sa in db.ServiceAssignments on s.Id equals sa.ServiceId
where sa.LocationId == 1
select s

어디 db당신입니다 DbContext. 생성 된 쿼리는 다음과 가변됩니다 (EF6의 샘플).

SELECT [Extent1].[Id] AS [Id]
       -- other fields from Services table
FROM [dbo].[Services] AS [Extent1]
INNER JOIN [dbo].[ServiceAssignments] AS [Extent2]
    ON [Extent1].[Id] = [Extent2].[ServiceId]
WHERE [Extent2].[LocationId] = 1

누구든지 메소드 구문에 관심이있는 경우 탐색 속성이 있으면 간단합니다.

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

그렇지 않다면, Join()내가 줄 알지 못하는 재정의 가 없다면 , 꽤 형편 없어 보인다고 생각합니다 (그리고 저는 메서드 구문 순수 주의자입니다).

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

가능한 경우 탐색 속성을 사용할 수 있습니다. SQL에서 내부 조인을 생성합니다.

from s in db.Services
where s.ServiceAssignment.LocationId == 1
select s

참고 URL : https://stackoverflow.com/questions/16025287/entity-framework-query-for-inner-join

반응형