1

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 个值。也许我错过了什么?

4

1 回答 1

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 {{{1}}})", GetFieldName(i), i);
   parameters.Add(words[i]);
}

var query = SQL
    .FROM("myTable")
    .WHERE("(" + predicate.ToString() + ")", parameters.ToArray());
于 2015-06-19T19:24:55.653 回答