English 中文(简体)
NHibernate: 如何利用“IN”创建标准?
原标题:NHibernate: How to use "IN" with CreateCriteria?

我有母子表关系:要素——(1-n)-> 内容栏。 每一条内容 环行有:独一无二的Id(Id)、ElementId、版本,然后是一些不太相关的领域。 I m 试图获得其最高版本(和Id)的所有内容。

我有这样一席之地,给我我希望:

SELECT * FROM ContentBlocks WHERE Id IN 
    (SELECT MAX(Id) FROM ContentBlocks GROUP BY ElementId)

(假设最新版本的Id最高)

然而,我可以说明如何从NHibernate那里获得。 这是我最接近的:

var subquery = DetachedCriteria.For<ContentBlock>()
            .SetProjection( Projections.Max( "Id" ) )
            .SetProjection( Projections.GroupProperty( "ElementId" ) );

var query = session.CreateCriteria<ContentBlock>()
            .Add( Subqueries.PropertyIn( "Id", subquery ) ).List<ContentBlock>();

如果我执行分局,我就在表格中找到一份独特的要素清单。 相反,我需要新内容提要清单。 如果我推翻了有关分局的分局预测顺序,那么我就达到了完成的任务。

我也试图这样做:

        subquery = DetachedCriteria.For<ContentBlock>()
            .SetProjection(
                Projections.ProjectionList()
                    .Add( Projections.Max( "Id" ) )
                    .Add( Projections.GroupProperty( "ElementId" ) )
                );

但是,我收到了一份清单,其中含有2个要素,即Id & ElementId,而创建标准会要求这样做。

是否有办法不将集团财产“ElementId”列入结果? 或者说,我要回去马克斯(Id)?

最佳回答
问题回答

暂无回答




相关问题
nHibernate one-to-many inserts but doesnt update

Instead of getting into code, I have a simple question. Default behavior for a simple one-to-many is that it inserts the child record then updates the foreign key column with the parent key. Has ...

How Do I copy an existing nhibernate object as a new object?

I a persisted NHibernate object that I would like to repersist as a new entity. How do I get NHibernate to save this object as if it was a new? I am thinking I might create a session interceptor to ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

NHibernate Search in a List using ICriteria

I have my class X : public class ClassX { public virtual IList<ClassY> ListY { get; set; } ... } My ClassX mapping (using Fluent) ... HasMany<ClassX>(x => x.ListY ) ....

热门标签