C# 3.0,Nhibernate 2.1.2,城堡ActiveRecord 2.1,WinXP 32
使用ActiveRecord和DetachedCriteria过滤元素有问题。有两个表,一个包含要过滤的对象(PropertyContainer),另一个包含为该对象(PropertyValue)设置的动态属性的值。
PropertyContainer
Id int
PropertyValue
Id int
ContainerId int
Value real我需要从PropertyContainer表中选择与某些条件相匹配的值的PropertyValue对象(例如Id =1和值>2的属性)。我想用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);然后执行此调用,我得到以下错误:“未能解析属性: ExternalProperty of: PropertyContainer”
问题是:
进行动态属性集过滤的正确方法是什么?
发布于 2011-06-15 12:56:24
如果PropertyValue看起来像:
class PropertyValue
{
public virtual int Id { get; set; }
public virtual double Value { get; set; }
}你可以:
DetachedCriteria.For<PropertyContainer>()
.CreateAlias("PropertyValues", "prop")
.Add(Restrictions.Ge("prop.Value", 2))
.Add(Restrictions.Eq("prop.Id", 1));https://stackoverflow.com/questions/6342918
复制相似问题