4

問題があります。リストexternalIdsにあるIDを持つ人のデータベースレコードから選択する必要があります。その後、すべての人に最新の StartTime を持つレコードを 1 つだけ選択する必要があります。たとえば、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>;
}

私の問題を解決する方法を知っている人はいますか? 前もって感謝します。

4

2 に答える 2

0

私も同様の問題を抱えていたので、誰かがそのようなものを望むなら、クエリを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;
}
于 2015-08-11T21:44:57.730 に答える