Only get latest results using nHibernate

后端 未结 4 1435
一整个雨季
一整个雨季 2020-12-12 03:31

I have a nHibernate query like this

ICriteria query = session.CreateCriteria(typeof(MyResult))
            .Add(Expression.Eq(\"ResultTypeId\", myResult.Resu         


        
相关标签:
4条回答
  • 2020-12-12 03:56

    You can order the result by ResultDate using the AddOrder method, as below:

    ICriteria query = session.CreateCriteria(typeof(MyResult))
            .Add(Expression.Eq("ResultTypeId", myResult.ResultTypeId))
            .AddOrder(Order.Desc("ResultDate"))
            .List<MyResult>();
    

    If you want to limit the number of MyResult instances you get back, you can use the SetMaxResults method, like so:

    ICriteria query = session.CreateCriteria(typeof(MyResult))
            .Add(Expression.Eq("ResultTypeId", myResult.ResultTypeId))
            .AddOrder(Order.Desc("ResultDate"))
            .SetMaxResults(20)
            .List<MyResult>();
    
    0 讨论(0)
  • 2020-12-12 03:57

    Order by ResultDate (descending) and select top whatever you feel appropriate.

    0 讨论(0)
  • 2020-12-12 03:58

    If I understand the question well, Mark wants to see an overview of all the last results for each type.

    Which means that, for every result type, he only wants to see only one row, and that is the Result which has last been added for that type.

    I think that, the easiest way to achieve this, would be to create an additional class, which we can call 'MyResultOverview' for instance:

    public class MyResultOverview
    {
        public int ResultId {get; set;}
        public int ResultTypeId {get; set;}
        public DateTime ResultDate {get; set;}
    }
    

    This class should not be mapped, but NHibernate should be aware that this class exists. Therefore, we'll have to import it:

    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" >
       <import class="MyResultOverview" />
    </hibernate-mapping>
    

    Then, we can create an ICriteria which will populate instances of MyResultOverview (and which will also generate the most efficient SQL Query in order to get this overview). It should look something like this:

    ICriteria criteria = session.CreateCritera (typeof(MyResult));
    
    criteria.SetProjection (Projections.ProjectionList ()
                               .Add (Projections.Property("Id"), "ResultId")
                               .Add (Projections.Property("ResultType"), "ResultType")
                               .Add (Projections.Max("ResultDate"), "ResultDate"));
    
    criteria.SetResultTransformer (Transformers.AliasToBean (typeof(MyResultOverview)));
    
    IList<MyResultOverview> results = criteria.List<MyResultOverview>();
    

    This should give you a list of MyResultOverview instances which represent the MyResults that you're looking for. Then, in order to retrieve the MyResult itself, you can simply do this by retrieving the MyResult instance for that particalur ResultId that you've retrieved as well.

    I haven't tested this, nor did i compile it, but this is the path that I would follow to achieve this.

    0 讨论(0)
  • 2020-12-12 04:09

    In HQLthis might work:

    select item, tag
    from MyItem item
        join item.Tags tag
    where tag.Id = (                                    
        select  max(tag2.Id)  
        from MyItem item2
            join item2.Tags tag2
        where item2.Id = item.Id
        group by item2.Id     
    )      
    
    0 讨论(0)
提交回复
热议问题