NHibernate的/ LINQ的:NHibernate.QueryException:无法解析属性:MyNamespace.MyObject:中Profile.class [英] Nhibernate/Linq: NHibernate.QueryException : could not resolve property: Profile.class of: MyNamespace.MyObject
问题描述
我有我的LINQ查询的问题。我试图筛选基于选择的值对象。我们使用它返回一个System.Linq.Expressions.Expression并使用它来创建NHibernate的查询的查询模式。这是我的LINQ表达式
I'm having an issue with my linq query. I am trying to filter objects based on selected values. We use a query model which returns a System.Linq.Expressions.Expression and uses it to create an nhibernate query. Here is my linq expression.
x =>(request.InitialLoad
|| (!request.InitialLoad
&& (Enum.GetValues(typeof(MyType)).Length == request.MyTypes.Length
||
(Enum.GetValues(typeof(MyType)).Length != request.MyTypes.Length
&&
((request.MyTypes.Contains((int)MyType.Referrals)
&& x.Post.PostType == StatusPostType.Referral)
||
(request.MyTypes.Contains((int)MyType.Businesses)
&& x.Post.Profile is BusinessProfile)
||
(request.MyTypes.Contains((int)MyType.Members)
&& x.Post.Profile is UserProfile)
)
)
)
)
)
&& x.Profile.Equals(request.Profile);
的映射(我们使用流利)是这样的:
The mappings (we are using fluent) Look like this:
为MyObject(邮政属性):
MyObject (Post property):
References(x => x.Post, "PostId");
MyObject.Post(配置文件属性):
MyObject.Post (Profile property):
References(x => x.Profile, "ProfileId");
在更改 x.Post.Profile是SOMETYPE
到 x.Post.Profile.GetType()== typeof运算(SOMETYPE)
,它抛出一个不同的错误,这是
When I change x.Post.Profile is SomeType
to x.Post.Profile.GetType() == typeof(SomeType)
it throws a different error, which is
System.ArgumentOutOfRangeException:
指数超出范围。必须是
非负并小于
中的集合的大小。参数名称索引:
System.ArgumentOutOfRangeException : Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
当我拿出了类型比较布尔表达式,只有在介绍人表达离开的时候,它工作正常进行过滤仅上一个选项。
When I take out the type comparison boolean expressions and only leave in the Referrals expression, it works fine for filtering only on that one option.
的属性不以任何方式被模型修改。他们是用默认的get / set虚拟属性。
The properties are not modified in any way by the model. They are virtual properties with the default get/set.
任何想法?
推荐答案
我敢肯定NHibernate.Linq不直接支持类类型过滤。如果你需要的类型来区分,我会在被设置为在子类中适当的值的基类(可能是一个枚举),使用属性值。然后,您可以执行这样的比较:
I'm pretty sure NHibernate.Linq does not support filtering on class type directly. If you need to distinguish between the types, I would use a property value on the base class (probably an enum) that is set to the proper value in the child classes. You can then perform your comparison like this:
x.Post.Profile.Type = ProfileTypes.BusinessProfile
在构造函数中的子类只需设置该属性静态的,与NHibernate映射它,并设置更新= FALSE
的属性映射。虽然有点不雅,这应该给你你要寻找的结果。
Just set this property statically in the constructor for the child classes, map it with NHibernate, and set update=false
for the property mapping. Although somewhat inelegant, this should give you the results you're looking for.
这篇关于NHibernate的/ LINQ的:NHibernate.QueryException:无法解析属性:MyNamespace.MyObject:中Profile.class的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!