NHibernate QueryOver 对变量的限制
我有一个小问题:我会在 QueryOver 中插入一个条件来检查变量值。像这样的东西:
var qOver = QueryOver.Of<MyModel>(() => myMod)
.JoinAlias(() => myMod.SubMod, () => subMod, JoinType.LeftOuterJoin)
.Where(Restrictions.Or(
Restrictions.On(() => myMod.ID).IsIn(MyIDList)
, Restrictions.On(MyIDList == null))
在 SQL 语法中,像
WHERE @Variable = '' OR MyTable.MyField = @Variable
这样,如果我的变量已填充,我将过滤我的字段。如果我的变量为空(或 null),我将选择每个记录而不过滤任何内容。
如何使用 QueryOver 和 Restrictions 达到此结果? 谢谢你!
I've a little problem: I would insert a condition into my QueryOver that checks also the variable value. Something like this:
var qOver = QueryOver.Of<MyModel>(() => myMod)
.JoinAlias(() => myMod.SubMod, () => subMod, JoinType.LeftOuterJoin)
.Where(Restrictions.Or(
Restrictions.On(() => myMod.ID).IsIn(MyIDList)
, Restrictions.On(MyIDList == null))
In SQL sintax something like
WHERE @Variable = '' OR MyTable.MyField = @Variable
So, if I my variable is filled I'll filter on my field. If my variable is empty (or null) I'll select every record without filter any content.
How can I reach this result using QueryOver and Restrinctions?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果变量为 null 或未设置,请勿将其添加到查询中。
If the variable is null or not set, dont add it to your query.