c# - NHibernate 左连接与 linq

标签 c# mysql linq join nhibernate

我需要将使用 NHibernate 的 linq 中的连接更改为左外连接。 我的代码如下所示

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;

现在我的要求是在此查询中使用左连接加入 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 

我在 NHibenate 或 linq 方面工作不多,所以不太了解如何在 NHibernate 或 linq 中进行左连接。 感谢任何帮助

最佳答案

目前不支持任意左连接 (v4.1) 。它们转换为 LINQ GroupJoin 一起使用时会抛出 NotImplementedException .

answer 中所写链接为 David ,您可以使用反而。普通也可以这样做(使用 theta 样式连接)。

您可以将仪表映射到服务点。它看起来像(没有您的 DimAccount 属性):

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

访问查询结果上的.ServicePointMeters 将触发最多 20 个已加载服务点集合的仪表集合延迟加载。这是由于我建议的映射中的 batch-size 属性造成的。如果没有它(并且没有全局配置的batch-size),它将一次仅触发一个集合的延迟加载,从而可能导致 n+1 性能问题。

如果您想立即加载它们,只需添加 fetch:

var spq = session.Query<DimServicePoint>()
    .Where(sp => sp.ServicePointID == servicePointID && sp.DimAccount.AccountKey != 0 &&
        sp.StartDayID <= currentDateId && sp.EndDayID >= currentDateId)
    .FetchMany(sp => sp.ServicePointMeters);

生成的查询将使用左连接。

注释:
我个人避免使用类似 LINQ sql 的语法,我喜欢使用 LINQ 扩展方法,如我的答案中所示。我发现它们更具可读性。
与 NHibernate 的急切加载相比,我更喜欢延迟加载,因为它可以批量加载延迟加载。正如我所解释的 here 它倾向于简化代码,同时保持良好的性能。 .

关于c# - NHibernate 左连接与 linq,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42410867/

相关文章:

c# - 如何将 FlowDocument 导出为 DOC(x) 或 XLS

c# - 如何证明某个版本的程序内存效率更高?

c# - DataVisualization.Chart 的图形和工具提示的样式化

mysql - 如何在 innodb 表中添加外键约束而不对列建立索引?

php - 修改和重命名 MySQL 查询中的 JSON 元素

LinqToSQl 和 Member 访问在类型异常上不合法

c# - 从基类返回派生类实例

php - 如何将 HTML 表单中的数组转换为字符串? [通知 : Array to string conversion?]

c# - LINQ 到 SQL : Left join on multiple columns

c# - Linq 到实体 : Count is very slow