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

查看:17
本文介绍了仅使用 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 来填充 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天全站免登陆