C# NHibernate与linq左连接
我需要将使用NHibernate的linq中的连接更改为左外连接。 我的代码是这样的C# NHibernate与linq左连接,c#,mysql,linq,join,nhibernate,linq-to-nhibernate,queryover,hql,C#,Mysql,Linq,Join,Nhibernate,Linq To Nhibernate,Queryover,Hql,我需要将使用NHibernate的linq中的连接更改为左外连接。 我的代码是这样的 IQueryable<DimServicePoint> spq = from sp in session.Query<DimServicePoint>() join spm in session.Query<DimServicepointMeter>() on sp
IQueryable<DimServicePoint> spq =
from sp in session.Query<DimServicePoint>()
join spm in session.Query<DimServicepointMeter>() on sp.ServicePointKey equals spm.ServicePointKey into gj
from subsp in gj.DefaultIfEmpty()
where (sp.ServicePointID == servicePointID && sp.DimAccount.AccountKey != 0 && sp.StartDayID <= currentDateId && sp.EndDayID >= currentDateId)
select sp;
IQueryable spq=
来自会话中的sp.Query()
将sp.ServicePointKey上的session.Query()中的spm加入gj
来自gj.DefaultIfEmpty()中的subp
其中(sp.ServicePointID==ServicePointID&&sp.dimcount.AccountKey!=0&&sp.StartDayID=currentDateId)
选择sp;
现在,我的要求是在这个查询中使用左连接连接DimServicepointMeter。
等效的SQL查询是:
select * from dw.dim_servicepoint sp
left join dw.dim_servicepoint_meter spm on sp.servicepointkey = spm.servicepointkey
where sp.servicepointid = @ServicePointID and sp.accountkey != 0
and sp.startdayid <= @currentDateId and sp.enddayid >= @currentDateId
从dw.dim_servicepoint sp选择*
左连接dw.dim\u servicepoint\u sp.servicepointkey=spm.servicepointkey上的仪表spm
其中sp.servicepointid=@servicepointid和sp.accountkey!=0
和sp.startdayid=@currentDateId
我在NHibenate或linq没有太多的工作,所以我不知道如何在NHibenate或linq中加入左撇子。
非常感谢您的帮助中当前不支持任意左连接(v4.1)。它们转换为LINQ,与一起使用时抛出
NotImplementedException
如链接方式中所述,您可以使用。普通也可以(使用θ样式联接)
您可以将仪表映射到服务点上。它看起来像(没有您的DimAccount
属性):
结果查询将使用左联接
注:我个人避免使用类似LINQSQL的语法,我喜欢使用LINQ扩展方法,如我的答案所示。我发现它们更具可读性。
我更喜欢使用NHibernate进行延迟加载,而不是急于加载,因为它可以批量加载延迟加载。正如我所解释的,它倾向于在保持良好性能的同时简化代码
public class DimServicePoint
{
public virtual int ServicePointID { get; set; }
public virtual int StartDayID { get; set; }
public virtual int EndDayID { get; set; }
public virtual int ServicePointKey { get; set; }
public virtual ISet<DimServicePointMeter> ServicePointMeters { get; set; }
}
public class DimServicePointMeter
{
public virtual int ServicePointMeterID { get; set; }
public virtual int ServicePointKey { get; set; }
}
<class name="DimServicePoint">
<id name="ServicePointID">
<generator class="assigned" />
</id>
<property name="StartDayID" />
<property name="EndDayID" />
<property name="ServicePointKey" />
<set name="ServicePointMeters" inverse="true" batch-size="20">
<key column="ServicePointKey" property-ref="ServicePointKey" />
<one-to-many class="DimServicePointMeter" />
</set>
</class>
<class name="DimServicePointMeter">
<id name="ServicePointMeterID">
<generator class="assigned" />
</id>
<property name="ServicePointKey" />
</class>
var spq = session.Query<DimServicePoint>()
.Where(sp => sp.ServicePointID == servicePointID && sp.DimAccount.AccountKey != 0 &&
sp.StartDayID <= currentDateId && sp.EndDayID >= currentDateId);
var spq = session.Query<DimServicePoint>()
.Where(sp => sp.ServicePointID == servicePointID && sp.DimAccount.AccountKey != 0 &&
sp.StartDayID <= currentDateId && sp.EndDayID >= currentDateId)
.FetchMany(sp => sp.ServicePointMeters);