여러 포함을 포함하는 linq 쿼리를 단순화하고 싶습니다.여러 포함을 포함하는 Linq 요청을 어떻게 나누는가?
내 모델은 간단합니다. 사이트는 하나의 계약서에 링크되어 있으며, 이는 하나의 클라이언트에 연결되어 있습니다. 그 고객은 전화, 우편 및 honorifics (appelRef) 하나의 요청을 받아야합니다.
요청 뒤에 있기 때문에 엔티티 프레임 워크에서 SQL Server 요청으로 변환되므로 단일 요청이 필요합니다.
var search =
from IMT.Site s in imtContext.IMTObjects.OfType<IMT.Site>()
.Include(
s => s.LienContratSiteRef
.Select(l => l.Contrat)
.Select(c => c.LienContratClientRef
.Select(l => l.Client)
.Select(cl => cl.Telephones)))
.Include(s => s.LienContratSiteRef
.Select(l => l.Contrat)
.Select(c => c.LienContratClientRef
.Select(l => l.Client)
.Select(cl => cl.Mails)))
.Include(s => s.LienContratSiteRef
.Select(l => l.Contrat)
.Select(c => c.LienContratClientRef
.Select(l => l.Client)
.Select(cl => cl.AppelRef)))
where s.Reference.ToString() == siteId
select s;
YOR가
.Include(
s => s.LienContratSiteRef
.Select(l => l.Contrat)
.Select(c => c.LienContratClientRef
.Select(l => l.Client)
..is 세 번 반복 된 블록을 알 수있다 : 여기
는 LINQ 요청이다. 코드 블록을 인수 분해하는 방법이 있습니까?업데이트 : Intermedray 개체 LienContratSiteRef 및 LienContratClientRef가 있고 관계가 0 - *이므로 LienContratSiteRef.Contrat 및 LienContratClientRef.Client는 컬렉션입니다.
.Include(
s => s.LienContratSiteRef
.Select(l => l.Contrat)
.Select(c => c.LienContratClientRef
.Select(l => l.Client)
.Select(cl => new { Tels = cl.Telephones, Mail = cl.Mails, Appel = cl.AppelRef})))
을하지만 그것은 런타임 오류 발생 :
나는 또한 시도
The Include path expression must refer to a navigation property defined on the type.
당신은 그렇게 할 수 있지만,이 코드는 나중에 유사한 경우에 재사용 될 수 있지만 (아주 일부 코드가 발생할 수있는 수동 표현을 포함 구축이 필요합니다 :이 메모리에 쿼리를 실행 한 후 나는 생각한다). – Evk