DbExtensions - 添加具有多个OR子句的Where子句

时间:2015-06-19 10:13:14

标签: c# sql asp.net .net dbextensions

DbExtensions的新手(就在今天早上),现在我有一个看起来像这样的SQL语句

SELECT * FROM myTable
WHERE (Field1 LIKE @Word)
    OR (Field2 LIKE @Word)
    OR (Field3 LIKE @Word)
    OR (Field4 LIKE @Word)
    OR (Field5 LIKE @Word)

我无法弄清楚如何使用DbExtensions做到这一点?

这是我到目前为止所拥有的

var query = SQL
    .FROM("myTable")
    .WHERE();

query.AppendClause("OR", ",", "Field1 LIKE {0}", new string[] { term });
query.AppendClause("OR", ",", "Field2 LIKE {0}", new string[] { term });
query.AppendClause("OR", ",", "Field3 LIKE {0}", new string[] { term });
query.AppendClause("OR", ",", "Field4 LIKE {0}", new string[] { term });
query.AppendClause("OR", ",", "Field5 LIKE {0}", new string[] { term });

但这不会增加很多参数,只有1个值。也许我错过了什么?

1 个答案:

答案 0 :(得分:0)

var predicate = new StringBuilder();
var parameters = new List<object>();

for (int = 0; i < words.Length; i++) {
   if (i > 0) {
      predicate.Append(" OR ");
   }
   predicate.AppendFormat("({0} LIKE {import sys  
reload(sys) 
sys.setdefaultencoding('utf8')
})", GetFieldName(i), i);
   parameters.Add(words[i]);
}

var query = SQL
    .FROM("myTable")
    .WHERE("(" + predicate.ToString() + ")", parameters.ToArray());