NHibernate结果转换器 - 不映射结果

时间:2014-11-19 19:54:11

标签: c# nhibernate queryover

当尝试使用NHibernate Queryover和AliasToBean转换器进行一些临时映射时,我在获取一组映射结果时遇到了一些麻烦。这似乎与我正在运行的SqlFunction投影相关,以15分钟的增量返回我的结果。如果我将查询更改为按日期分组,我会得到预期的结果,但是在执行自定义SqlFunction时,映射的结果不是查询在执行时返回的结果。

我已经尝试将问题分解为最简单的形式,但我仍然无法将结果正确映射。使用SQL事件探查器,我可以看到正在创建正确的查询。我不太确定问题是否与AliasToBean转换器或我的自定义SqlFunction投影有关。

这是一个愚蠢的例子,显示我遇到的问题。

public class MyModel {
     public virtual int Id { get; set; }
     public virtual DateTime ProcessDate {get; set; }
}
public class MyModelMap : ClassMap<MyModel> {
    public MyModelMap() {
        Id(x => x.Id);
        Map(x => x.ProcessDate)
    }
}

public class MySummaryModel {
    public virtual DateTime ProcessDate { get; set; }
    public virtual int Totals { get; set; }
}

public void Test() {

    MyModel model = null;
    var query = session.QueryOver<SubmitterFile>(() => model)
                        .Where(() => model.ProcessDate >= DateTime.Now.Date);

    MySummaryModel summary = null;

    var mSelectList = new QueryOverProjectionBuilder<MyModel>();

    IProjection mTimeProjection;
    var mFunctionTemplate = new SQLFunctionTemplate(NHibernateUtil.DateTime2, "DATEADD(MINUTE, -1 * DATEDIFF(MINUTE, 0, ?1) % 15, DATEADD(MINUTE, DATEDIFF(MINUTE, 0, ?1), 0))");
    mTimeProjection = Projections.GroupProperty(Projections.SqlFunction(mFunctionTemplate, NHibernateUtil.DateTime2, Projections.Property(() => model.ProcessDate))).WithAlias(() => summary.ProcessDate);

/* Changing my mTimeProjection to the following works, giving me the full summary for the day, but not broken into 15 minute intervals
mTimeProjection = Projections.GroupProperty(Projections.Cast(NHibernateUtil.Date, Projections.Property(() => model.ProcessDate))).WithAlias(() => summary.ProcessDate);
*/

    mSelectList = mSelectList
                        .Select(mTimeProjection)
                        .Select(Projections.Count(() => model.Id).WithAlias(() => summary.Totals));

    var results = query.SelectList(x => mSelectList)
                        .TransformUsing(Transformers.AliasToBean<MySummaryModel>())
                        .List<MySummaryModel>();

}

有没有人有任何建议可以帮我弄清楚为什么结果没有被映射?

[编辑]

NHibernate版本:3.4.0.0

流利的NHibernate:1.4

以下是生成的查询,该查询在SSMS中按预期运行。

exec sp_executesql N'SELECT DATEADD(MINUTE, -1 * DATEDIFF(MINUTE, 0, this_.ProcessDate) % 15, DATEADD(MINUTE, DATEDIFF(MINUTE, 0, this_.ProcessDate), 0)) as y0_, count(this_.ID) as y1_ FROM MyModel this_ WHERE this_.ProcessDate >= @p0 GROUP BY DATEADD(MINUTE, -1 * DATEDIFF(MINUTE, 0, this_.ProcessDate) % 15, DATEADD(MINUTE, DATEDIFF(MINUTE, 0, this_.ProcessDate), 0))',N'@p0 datetime',@p0='2014-11-19 00:00:00'

从NHibernate返回的结果是:(JSON格式化以便于阅读)

[{
     DateTime: 11/19/2014 7:30:00 AM,
     Totals: 1
},{
     DateTime: 11/19/2014 9:30:00 AM,
     Totals: 1
}]

虽然这些时间戳确实存在于数据库的结果中,但总计值不与它对齐。在数据库上运行实际查询显示自午夜以来每15分钟的结果(到目前为止65个时间点)。每15分钟的时间戳,Totals列中有1到几百个。

0 个答案:

没有答案