使用 ActiveRecord、NHibernate、DetachedCriteria 过滤对象
C# 3.0、Nhibernate 2.1.2、Castle ActiveRecord 2.1、WinXP 32
我在使用 ActiveRecord 和 DetachedCriteria 过滤元素时遇到问题。 有两个表,一个包含要过滤的对象(PropertyContainer),另一个包含为此对象设置的动态属性值(PropertyValue)。
PropertyContainer
Id int
PropertyValue
Id int
ContainerId int
Value real
我需要选择 PropertyContainer 对象,其中 PropertyValue 表中的值与某些条件匹配(例如,Id = 1 且 Value > 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);
然后执行此调用我收到以下错误: “无法解析属性:PropertyContainer 的ExternalProperty”
问题是:
- 这种方法有什么问题?
- 使用 ActiveRecord/NHibernate 和 DetachedCriteria 按动态属性集进行过滤的正确方法是什么?
C# 3.0, Nhibernate 2.1.2 , Castle ActiveRecord 2.1, WinXP 32
I have a problem filtering elements with ActiveRecord and 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).
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 (e.g. property with Id = 1 and Value > 2). I would like to do this using DetachedCriteria, I am trying to write something like this:
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"
The question is:
- What is wrong with this approach ?
- What is the right way to do filtration by dynamic property set using ActiveRecord/NHibernate and DetachedCriteria ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果 PropertyValue 看起来像:
你可以这样做:
if PropertyValue looks like:
you can do: