English 中文(简体)
• 如何“无”地预测NHibernate?
原标题:How to "undo" a Projection in Fluent NHibernate?

我必须展示一些储存在关系数据库中的物体,我使用流利的NHibernate来接收这些物体。

自2006年以来 我需要花时间,我必须同时拿到——所有物体的计数和本页本身的物体。

The ICriteria for both purposes is very similar up to a point - for count i finally add .SetProjection(Projections.RowCount()) and for current object list I add SetFirstResult, AddOrder and SetMaxResults

难道我可以不去预测结果本身的标准和再利用标准,或者我是否必须为此目的重建标准?

hibernateForum建议采用一种不可行的方式。

问题回答

我将写出一种方法,概述包括限制、分类在内的质疑逻辑。

public DetachedCriteria GetCriteria()
{
    return DetachedCriteria.For<Entity>()
        .Add(Restrictions.Eq(...))
        .Add(...);
}

然后向数据库发出要求:

var count = GetCriteria()
    .GetExecutableCriteria(session)
    .SetProjection(Projections.Count(Projections.Id()))
    .UniqueResult<int>();

var result = GetCriteria()
    .GetExecutableCriteria(session)
    .SetFirstResult(0) 
    .SetMaxResults(10)
    .List<Entity>();

为进一步优化这项工作,您可研究这一出色的博客:post

public static DetachedCriteria Clone(this DetachedCriteria criteria)
{
   var dummy = criteria.ToByteArray();
   return dummy.FromByteArray<DetachedCriteria>();
}
var criteria = GetCriteria()
var count = criteria 
    .Clone()
    .GetExecutableCriteria(session)
    .SetProjection(Projections.Count(Projections.Id()))
    .UniqueResult<int>();

var result = criteria 
    .GetExecutableCriteria(session)
    .SetFirstResult(0) 
    .SetMaxResults(10)
    .List<Entity>();




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签