0

私はC#の初心者です。

クエリ文字列を作成したいのですが、いくつかの条件を実行し、すべての条件が where 句に別の条件を追加します

私はそのようなものが欲しい:

    // BUILD SELECT QUERY
     string where = "";
     string[] where_arr = new string[];
     if (condition1)
     {
           where_arr[index++] = " field = 5 ";
     }
      if (condition2)
     {
           where_arr[index++] = " field2 = 7 ";
     }

     if (where_arr.Count>0)
        where = " where" +  String.Join(" and ", where_arr);
     string sql = "select count(*) as count from mytable " + where;

しかし、次のようなすべての変数を宣言する方法が正確にはわかりませんwhere_arr

4

1 に答える 1

1
// BUILD SELECT QUERY
string where = "";
List<string> where_arr = new List<string>();

if (condition1)
{
    where_arr.Add(" field = 5 ");
}

if (condition2)
{
    where_arr.Add(" field2 = 7 ");
}

if (where_arr.Count > 0)
    where = " where" + String.Join(" and ", where_arr.ToArray());
string sql = "select count(*) as count from mytable " + where;
于 2010-11-23T20:18:51.693 に答える