简体   繁体   English

NHibernate查询最新记录

[英]NHibernate query for most recent records

Ok, this should be really simple. 好的,这应该很简单。 I'm new to NHibernate, but everywhere I've searched has given me conflicting answers on this, and none of them seem to work. 我是NHibernate的新手,但是在我搜索过的所有地方都给了我与此矛盾的答案,而且似乎都没有用。 Basically, I need to implement the following SQL query into my code. 基本上,我需要在代码中实现以下SQL查询。 It simply returns the most recent records for a specific participant. 它仅返回特定参与者的最新记录。

select *
from result
where ParticipantId = 1
and ResultDate = (select MAX(resultDate)
                  from Result
                  where ParticipantId = 1)

I'm using Fluent NHibernate with NHibernate 3.3. 我在NHibernate 3.3中使用Fluent NHibernate。 I can pull back the data without the "and" in the where clause using the code below, but when I try to restrict it to the most recent records, nothing's working. 我可以使用下面的代码在where子句中不使用“ and”来提取数据,但是当我尝试将其限制为最新记录时,则无济于事。

var resultSet = session.Query<Result>()
.Where(r => r.Participant.ParticipantId == 1);

Also including my domain and mapping files here for completeness. 为了完整性,还包括我的域和映射文件。 Let me know if more info is needed. 让我知道是否需要更多信息。

public class Result
{
    public virtual int ResultId { get; set; }
    public virtual Participant Participant { get; set; }
    public virtual ResultType ResultType { get; set; }
    public virtual float? ResultValue { get; set; }
    public virtual string ResultText { get; set; }
    public virtual DateTime ResultDate { get; set; }
    public virtual int? Systolic { get; set; }
    public virtual int? Diastolic { get; set; }
    public virtual DateTime? DateAdded { get; set; }
    public virtual string AddedBy { get; set; }
    public virtual DateTime? DateChanged { get; set; }
    public virtual string ChangedBy { get; set; }
}

public ResultMap() 
{
    Table("Result");
    LazyLoad();
    Id(x => x.ResultId).GeneratedBy.Identity().Column("ResultId");
    References(x => x.Participant).Column("ParticipantId");
    References(x => x.ResultType).Column("ResultTypeId");
    Map(x => x.ResultValue).Nullable();
    Map(x => x.ResultText).Nullable().Length(100);
    Map(x => x.ResultDate).Nullable();
    Map(x => x.Systolic).Nullable();
    Map(x => x.Diastolic).Nullable();
    Map(x => x.DateAdded).Nullable();
    Map(x => x.AddedBy).Nullable().Length(100);
    Map(x => x.DateChanged).Nullable();
    Map(x => x.ChangedBy).Nullable().Length(100);
}

Any thoughts on an easy solution to get the recent data? 对获取最近数据的简单解决方案有何想法? Bonus points for letting me know how to get the last 2 dates, as I'll need them soon for comparing improvements. 奖励积分,让我知道如何获得最近的2个日期,因为我很快就会需要它们来比较改进。 Thanks in advance! 提前致谢!

To get that same query via QueryOver... 要通过QueryOver获得相同的查询...

var maxResultDate = QueryOver.Of<Result>()
    .Where(x => x.Participant.ParticipantId  == 1)
    .Select(Projections.Max<Result>(x => x.ResultDate));

var rslt = s.QueryOver<Result>()
    .Where(x => x.Participant.ParticipantId == 1)
    .WithSubquery.WhereProperty(x => x.ResultDate).Eq(maxResultDate)
      //or to get the "last" 2 rows...
      //.OrderBy(x => x.ResultDate).Desc
      //.Take(2)
    .List();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM