【问题标题】:NHibernate Criteria - how correctly use group by statement?NHibernate Criteria - 如何正确使用 group by 语句?
【发布时间】:2013-08-26 02:58:56
【问题描述】:

我有问题。我需要从数据库记录中选择具有列表 externalIds 中 id 的人员。之后,我只需要为每个人选择 1 条具有最新 StartTime 的记录。例如,我尝试使用 SetProjection(GroupProperty 和 Max 属性),但结果是当我需要 PersonnelPresence 列表时,它只返回 StartTime 列表。我的方法如下:

public IList<PersonnelPresence> GetLastPersonnelPresencesForPeopleExternalIds(IList<string> externalIds)
{
            ICriteria criteria = Session.CreateCriteria(typeof(PersonnelPresence), "pp").CreateCriteria("pp.Person", "p")
                .Add(Restrictions.In("p.ExternalId", externalIds.ToList()))
                .SetProjection(Projections.ProjectionList()
                .Add(Projections.GroupProperty("p.Id"))
                .Add(Projections.Max("pp.StartTime")));

            return criteria.List<Object>() as List<PersonnelPresence>;
}

有人知道如何解决我的问题吗?提前致谢。

【问题讨论】:

    标签: c# nhibernate criteria


    【解决方案1】:

    已经很久了,但我仍然想尝试并提供一些帮助。

    对于诸如此类的复杂查询,我发现先键入 SQL 会有所帮助;一旦您有了能够为您提供所需结果的 SQL 语句,就可以更轻松地将查询转换为 ICriteria。

    如果可能,我建议使用 FluentNHibernate 的 Query(而不是 QueryOver)语法,因为这样更容易维护,并且对于像您这样的复杂情况更灵活。

    【讨论】:

      【解决方案2】:

      我有类似的问题,所以如果有人想要这样的东西,我设法通过将查询分成两部分来解决。

      类似的东西:

      public IList<PersonnelPresence> GetLastPersonnelPresencesForPeopleExternalIds(IList<string> externalIds)
      {
          var criteriaPersonnelPresenceNewest = DetachedCriteria.For<PersonnelPresence>("PP_")
                                                      .Add(Restrictions.In("PP_.ExternalId", externalIds.ToArray()()))
                                                      .Add(Restrictions.EqProperty("PP_.ExternalId", "PP2_.ExternalId"))//to compare with the query down below
                                                      .SetProjection(Projections.ProjectionList()
                                                                                .Add(Projections.Max("PP_.StartTime"))
                                                      );
      
      var criteriaPersonnelPresence = Session.CreateCriteria<PersonnelPresence>("PP2_")                                                   
                                              .Add(Subqueries.PropertyEq("PP2_.StartTime", criteriaPersonnelPresenceNewest))
                                              .SetProjection(Projections.ProjectionList()
                                                                        .Add(Projections.Property("PP2_.Id"))
                                                                        .Add(Projections.Property("PP2_.StartTime"))
                                              )
                                              .ToList<PersonnelPresence>();
      
      return criteriaPersonnelPresence;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-10-07
        • 2011-04-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多