仅使用nHibernate获得最新结果 [英] Only get latest results using nHibernate

查看:69
本文介绍了仅使用nHibernate获得最新结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的nHibernate查询

I have a nHibernate query like this

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

问题是用户一直在添加结果,我想显示一张表,显示我拥有的所有不同ResultType的所有最新结果.

Problem is that users add results all the time and I want to show a table of all the latest results for all the diferent ResultTypes I have.

MyResult类具有ResultDate属性.我的问题是,如何添加查询以使其仅返回给定结果类型的最新结果.没有什么可说的是,结果将按日期顺序显示在数据库中.

The MyResult class has a property ResultDate. My question is, what do I add to the query to get it to only return the latest result for the given result type. There is nothing to say that the results will be in date order in the database.

谢谢

标记

推荐答案

如果我很好地理解了这个问题,Mark希望查看每种类型的所有最后结果的概述.

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.

我认为,最简单的方法是创建一个额外的类,例如,我们可以将其称为"MyResultOverview":

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;}
}

不应该映射此类,但是NHibernate应该知道此类存在.因此,我们必须将其导入:

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>

然后,我们可以创建一个ICriteria,该ICriteria将填充MyResultOverview的实例(并且还将生成最有效的SQL查询以获取此概述). 它应该看起来像这样:

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>();

这应该为您提供MyResultOverview实例的列表,这些实例表示您要查找的MyResults. 然后,为了检索MyResult本身,只需为已检索到的特定ResultId检索MyResult实例即可.

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.

这篇关于仅使用nHibernate获得最新结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆