我的 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
to x.Post.Profile.GetType() == typeof(SomeType)
它会抛出一个不同的错误,即
System.ArgumentOutOfRangeException:
指数超出范围。必须是
非负且小于
集合。参数名称:索引
当我取出类型比较布尔表达式并仅保留引用表达式时,它可以很好地仅过滤该选项。
模型不会以任何方式修改这些属性。它们是具有默认获取/设置的虚拟属性。
有任何想法吗?
我很确定 NHibernate.Linq 不支持直接对类类型进行过滤。如果您需要区分类型,我将在基类(可能是枚举)上使用属性值,该属性值在子类中设置为正确的值。然后您可以像这样进行比较:
x.Post.Profile.Type = ProfileTypes.BusinessProfile
只需在子类的构造函数中静态设置此属性,将其映射到 NHibernate,然后设置update=false
用于属性映射。尽管有些不雅,但这应该会给您带来您想要的结果。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)