2010-11-23 4 views
0

Je suis novice en C#,C# aide à la construction clause where pour interroger

je veux construire la chaîne de requête, je fais certaines conditions, toutes les conditions ajouter une autre condition à l'endroit où la clause

je veux quelque chose comme ça :

// 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; 

mais je ne sais pas exactement comment déclarer toutes les variables comme where_arr

Répondre

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; 
+0

merci, son travail –