我需要按如下方式进行选择
select * from produtos where Value = 10 or Value = 15 or Value= 20 ....因为我不知道会有多少值,所以它将在一个列表上循环,用户将决定将是多少值……问题是,如果我用标准
ICriteria criterion = session.createCriteria(typeof (Product) "produto"). SetCacheable (true);
criterio.Add (Restrictions.Eq ("produto.Valor", 10));
criterio.Add (Restrictions.Eq ("produto.Valor", 15));
criterio.Add (Restrictions.Eq ("produto.Valor", 20));由select子句"and“构成
select * from produtos where Value=10 and Value = 15 and Value= 20 ....不能使用限制" in“,因为我可以在Restrictions.Eq Restrictions.Ge或Restrictions.Le或任何其他子句中有位置...
在标准中添加子句还有什么方法吗?就像这样
criteria.Add (Restrictions.Or (Restrictions.Eq ("produto.Valor", 10)));
criteria.Add (Restrictions.Or (Restrictions.Eq ("produto.Valor", 15)));
criteria.Add (Restrictions.Or (Restrictions.Eq ("produto.Valor", 20)));
criteria.Add (Restrictions.Or (Restrictions.Eq ("produto.Valor", 25)));我知道它使用了像link这样的表达式,但不明白这如何帮助我使用select,例如,假设我有一个foreach,并且对于每个项目,我需要在条件中添加一个"or“,
foreach (var item in items)
{
criteria.Add("or item.Valor =" item.Valor);
}我只能以此为标准:
foreach (var item in items)
{
criteria.Add(Restrictions.Eq("item.Valor", item.Valor));
}什么是“和”或者它。但这不会或可能会增加另一个标准。
我也希望在同样的情况下
foreach (var item in items)
{
var items = session.QueryOver<Item>()
.WhereRestrictionOn(c => c.Valor item.Valor == | |?)
.List<Item>();
}发布于 2013-02-15 20:31:15
也许你可以试试:
criteria.Add (
new Disjunction()
.Add (Restrictions.Eq ("produto.Valor", 10))
.Add (Restrictions.Eq ("produto.Valor", 15))
.Add (Restrictions.Eq ("produto.Valor", 20))
.Add (Restrictions.Eq ("produto.Valor", 25))
);我猜criteria.Add (Restrictions.In("produto.Valor", new[]{10,15,20,25});也应该可以工作
希望这能有所帮助
https://stackoverflow.com/questions/14892611
复制相似问题