简体   繁体   English

使用 ActiveRecord,NHibernate, DetachedCriteria 过滤对象

[英]Filtering objects using ActiveRecord,NHibernate, DetachedCriteria

C# 3.0, Nhibernate 2.1.2, Castle ActiveRecord 2.1, WinXP 32 C# 3.0,Nhibernate 2.1.2,城堡 ActiveRecord 2.1,WinXP 32

I have a problem filtering elements with ActiveRecord and DetachedCriteria.我在使用 ActiveRecord 和 DetachedCriteria 过滤元素时遇到问题。 There are 2 tables one contains the objects to be filtered (PropertyContainer) and the other contains the values of dymamic property set for this object (PropertyValue).有 2 个表,一个包含要过滤的对象 (PropertyContainer),另一个包含为此 object (PropertyValue) 设置的动态属性值。

PropertyContainer
 Id int

PropertyValue
 Id             int
 ContainerId    int
 Value          real

I need to select PropertyContainer object with the values from PropertyValue table matching some condition (eg property with Id = 1 and Value > 2).我需要 select PropertyContainer object 与 PropertyValue 表中的值匹配某些条件(例如,Id = 1 和 Value > 2 的属性)。 I would like to do this using DetachedCriteria, I am trying to write something like this:我想使用 DetachedCriteria 来执行此操作,我正在尝试编写如下内容:

var detachedCriteria = DetachedCriteria.For(typeof(PropertyContainer));

detachedCriteria.SetProjection(
    Projections.SqlProjection(@"select Value from PropertyValue where Id=1"),
    new[] { "ExternalProperty" }, 
    new[] { NHibernateUtil.Double }));  

detachedCriteria.Add(Expression.Ge("ExternalProperty",2));

var filteredItems = PropertyContainer.SlicedFindAll(0,100,detachedCriteria);

Then this call is executed I get the following error: "could not resolve property: ExternalProperty of: PropertyContainer"然后执行此调用我收到以下错误:“无法解析属性:ExternalProperty of:PropertyContainer”

The question is:问题是:

  1. What is wrong with this approach?这种方法有什么问题?
  2. What is the right way to do filtration by dynamic property set using ActiveRecord/NHibernate and DetachedCriteria?使用 ActiveRecord/NHibernate 和 DetachedCriteria 通过动态属性集进行过滤的正确方法是什么?

if PropertyValue looks like:如果 PropertyValue 看起来像:

class PropertyValue
{
    public virtual int Id { get; set; }
    public virtual double Value { get; set; }
}

you can do:你可以做:

DetachedCriteria.For<PropertyContainer>()
    .CreateAlias("PropertyValues", "prop")
    .Add(Restrictions.Ge("prop.Value", 2))
    .Add(Restrictions.Eq("prop.Id", 1));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM