在我的项目中,我尝试建立这样的标准:
ICriteria criteria = session.CreateCriteria(typeof(Orders), typeof(Orders).Name);
if (param != null)
{
if (param[1] != "System")
{
for (int index = 0; index < param.Length - 1; index++)
{
if (!string.IsNullOrEmpty(param[index]))
{
criteria.Add(Expression.Eq(
"RealizationPoint",
CommonUtils.GetNameRealizationPointById(param[index])));
}
}
}
if (param[1] != "System" && param2 != null &&
!string.IsNullOrEmpty(param2[0]))
{
for (int index = 0; index < param2.Length - 1; index++)
{
if (!string.IsNullOrEmpty(param2[index]))
{
criteria.Add(Expression.Eq(
"RealizationPoint",
CommonUtils.GetNameRealizationPointById(param2[index])));
}
}
}
}
param1,param2:string [] param1,string [] param2。 在表达式保持AND之间的结果中,我需要OR。
答案 0 :(得分:1)
您可以使用Restrictions.Disjunction()
将多个表达式分组为OR,将Restrictions.Conjuctions()
分组为AND。
简化示例:
var conjunction1 = Restrictions.Conjunction();
for (int index = 0; index < param.Length -1; index++)
{
conjunction1.Add(Restrictions.Eq("RealizationPoint", param[index]));
}
var conjunction2 = Restrictions.Conjunction();
for (int index = 0; index < param2.Length -1; index++)
{
conjunction2.Add(Restrictions.Eq("RealizationPoint", param2[index]));
}
criteria.Add(Restrictions.Disjunction().Add(conjunction1).Add(conjunction2));
// Or with Restrictions.Or()
// criteria.Add(Restrictions.Or(conjunction1, conjunction2));
您可以在此SO问题中找到更多信息和替代方案:How to create OR statements for NHibernate?