我的 linq 查询遇到问题。我正在尝试根据选定的值过滤对象。我们使用返回 System.Linq.Expressions.Expression 的查询模型,并使用它来创建 nhibernate 查询。这是我的 linq 表达式。
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);
映射(我们使用 Fluent)如下所示:
MyObject(Post 属性):
References(x => x.Post, "PostId");
MyObject.Post(配置文件属性):
References(x => x.Profile, "ProfileId");
当我将 x.Post.Profile is SomeType
更改为 x.Post.Profile.GetType() == typeof(SomeType)
时,它会引发不同的错误,是
System.ArgumentOutOfRangeException : Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
当我取出类型比较 bool 表达式并仅保留引用表达式时,它可以很好地仅针对该一个选项进行过滤。
模型不会以任何方式修改属性。它们是具有默认 get/set 的虚拟属性。
有什么想法吗?
最佳答案
我很确定 NHibernate.Linq 不支持直接对类类型进行过滤。如果您需要区分类型,我将在基类(可能是枚举)上使用属性值,该属性值在子类中设置为正确的值。然后您可以像这样进行比较:
x.Post.Profile.Type = ProfileTypes.BusinessProfile
只需在子类的构造函数中静态设置此属性,使用 NHibernate 进行映射,并为属性映射设置 update=false
即可。虽然有些不雅,但这应该会给您带来您正在寻找的结果。
关于c# - Nhibernate/Linq : NHibernate. QueryException : could not resolve property: Profile. : MyNamespace. MyObject 的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3451395/