我有这个表结构:
ReferralSource - main table
-Phone - join table Rs as one-to-one
- Carrier - child table for Phone
- Type - child table for Phone
我想通过Linq查询得到它:
Session.Query<ReferralSource>()
.Fetch(x => x.Phone)
.ThenFetch(x => x.Type)
.Fetch(x => x.Phone)
.ThenFetch(x => x.Carrier);
此查询获取此SQL代码:
select referralso0_.Id,
referralso0_.FirstName,
phonetype4_.TypeName
carrier6_.Name
from REFERRALSOURCES referralso0_
left outer join PHONES phone3_
on referralso0_.PhoneId = phone3_.Id
left outer join PHONETYPES phonetype4_
on phone3_.TypeId = phonetype4_.Id
- duplicated join started
left outer join PHONES phone5_
on referralso0_.PhoneId = phone5_.Id
left outer join CARRIERS carrier6_
on phone5_.CarrierId = carrier6_.Id - duplicated join finished
如何使用fetch删除重复的左连接?
答案 0 :(得分:1)
如果您将Phone映射为组件而不是实体
,则可能public ReferralSourceMap()
{
Join("Phones", join =>
{
join.KeyColumn("...");
join.Component(x => x.Phone, c => { ... });
});
}
编辑:不幸的是,只有使用Criteria和QueryOver才能使用LINQ2NHibernate。我应该删除答案吗?