如何在nHibernate中的两个查询位置连接两个表

时间:2018-07-14 10:06:10

标签: linq nhibernate queryover

我有两个类 ComRecordMain ComRecordFeeder

//Tabel name is TBL_RecordMains
public class ComRecordMain {
    public virtual long Id { get; set; }            //Key, Auto increment
    public virtual ComDevice Device { get; set; }   //Column name is 'deviceId'
    public virtual int Date { get; set; }
    public virtual int Hour { get; set; }
    public virtual float Stotal { get; set; }
    public virtual float In { get; set; }           //Column name is '_in'
    public virtual int Unbalance { get; set; }
}

//Tabel name is TBL_RecordFeeders
public class ComRecordFeeder {
    public virtual ComRecordMain MainRecord { get; set; }   //Composite Key
    public virtual int FeederIndex { get; set; }            //Composite Key
    public virtual float Stotal { get; set; }
    public virtual float In { get; set; }                   //Column name is '_in'
    public virtual int Unbalance { get; set; }
}

我需要分析:

  

如果Feeder中每天最多,请为每个Feeder选择不平衡

我尝试为此T-Sql创建nHibernate查询:

SELECT 
    date, 
    [FeederIndex], 
    max([Unbalance]) as [Unbalance]
FROM 
    (
        SELECT 
            T1.date,
            T1.[Unbalance], 
            T1.[FeederIndex] 
        FROM 
            (
                SELECT 
                    R1.date,
                    R2.Stotal,
                    R2.[FeederIndex], 
                    R2.[Unbalance]
                FROM 
                    [TBL_RecordMains] AS R1 
                    JOIN
                    [TBL_RecordFeeders] AS R2 
                    ON
                    R1.ID = R2.Id
                WHERE 
                    deviceId = 30 AND[date] >= 35064 AND[date] <= 35428 
            ) AS T1
            JOIN
            (
                SELECT 
                    date, 
                    FeederIndex, 
                    max(W2.stotal) AS smax 
                FROM 
                    [TBL_RecordMains] AS W1 
                    JOIN
                    [TBL_RecordFeeders] AS W2 
                    ON
                    W1.Id = W2.Id
                WHERE 
                    deviceId = 30 AND[date] >= 35064 AND[date] <= 35428 
                GROUP by
                    [Date],
                    [FeederIndex]
            ) AS T2
            ON
                T1.date = T2.date AND 
                T1.FeederIndex = T2.FeederIndex AND 
                T1.Stotal = T2.smax
    ) AS T3
Group by 
    date, 
    [FeederIndex]

现在,我编写了最小的NHibernate查询来获取所有数据,并使用linq进行聚合并计算结果。

  • 我需要从数据库中获取大量数据
  • 数据中的许多linq查询

用于创建我的结果。
如何将此SQL转换为NHibernate查询?

0 个答案:

没有答案