我在同一张桌子上使用连接三次:
var queryResults = from avse in agreementVacationApplicationDomainContext.AVSEs
join utyp in agreementVacationApplicationDomainContext.UTYPs on avse.UTYPId equals utyp.Id
join ktabAgreement in agreementVacationApplicationDomainContext.KTABs on avse.KtabAVTALId equals ktabAgreement.Id
join ktabVacation in agreementVacationApplicationDomainContext.KTABs on avse.KTABVacationGroupId equals ktabVacation.Id
join ktab in agreementVacationApplicationDomainContext.KTABs on avse.KTABId equals ktab.Id
where ktab.KTABTOM >= DateTime.Today
select new
{
avse.Id,
ktabId = ktab.Id,
ktabVacationId = ktabVacation.Id,
ktabAgreementId = ktabAgreement.Id,
utypId = utyp.Id,
EmployementAgreement = ktabAgreement.KTABINIT + " " + ktabAgreement.KTXTLANG,
VacationGroup = ktabVacation.KTABINIT + " " + ktabVacation.KTXTLANG,
KtabCode = ktab.KTABINIT + " " + ktab.KTXTLANG,
FieldName = utyp.UPPGTYP
};
有没有更好的方法来实现此查询?
答案 0 :(得分:2)
是:不使用联接,但使用导航属性。而不是
join utyp in agreementVacationApplicationDomainContext.UTYPs on avse.UTYPId equals utyp.Id
您应该只使用avse.UTYP
(在原始查询中等同于utyp
)。这仍然是一个封闭的联接,但自动。如果您正确映射模型,则可以使用此类导航属性。
需要使用ORM和LINQ的连接的情况数量非常小,因为大多数表关系都可用作导航属性。