2012-04-16 41 views
0

我正在編寫一個需要執行搜索的程序作爲所需功能之一。用戶應該能夠使用任何數量的字段,從無到全部(總數爲7)。到目前爲止,我已經成功中分組數據輸入的各種情況下的if語句,像這樣:在IF語句中對多個條件進行分組C#

List<TypeClass> myList = new List<TypeClass> 

foreach TypeClass currentItem in myList 

{ 
    if (data1 == currentItem.GetData1() || data1 == "Do Not Search" && (data2 == currentItem.GetData2() || data2 == "Do Not Search") && (data3...) 

    { 
     //Stuff 
    } 

} 

如果你注意到,我分組括號內的每個數據字段,這樣的聲明只能滿足如果每個的輸入的數據是所需的條件,或者是一個「空場」。但是,我不能將聲明的第一部分與其他數據進行分組2,3,4 ...相反,即使其他搜索字段不滿足條件該聲明。如果我使用額外的括號,程序完全忽略了if語句並將其視爲沒有任何一個匹配。

所以,如果我把它寫這樣的:

if ((data1 == currentIten.GetData1 || data1 == "Do Not Search") && (data2...) 

沒有得到確認,該語句將被忽略。這是正常的嗎?有沒有更好/更有效的方法來處理可選的搜索字段選擇?

編輯:很抱歉的錯字,每個GetDataX是一個訪問,我忘了寫括號()

+0

我會猜測你的代碼之外有什麼事情正在發生。你確定'data1'設置正確嗎? – climbage 2012-04-16 23:02:37

+0

是的,所有的數據都在那裏,這是一個實際檢查它的問題,這是我現在的問題。如果我在開始時取出雙括號並僅替換一個括號,那麼一切正常,但是沒用,因爲如果數據1的計算結果爲true,但其他數據不是,則WHOLE語句的計算結果爲true,無論如何。 – 2012-04-16 23:06:46

+0

當然,您需要圍繞data1條件進行括號。仔細檢查值。其中一個dataX不匹配,將整個表達式評估爲false。重寫是否作爲一系列嵌套ifs,逐步瀏覽並查看不匹配的位置。 – 2012-04-16 23:08:46

回答

2

你可以做這樣的一個或條件

 List<string> mylist = new List<string>(); 
     string data1 = "test1"; 
     string data2 = "test2"; 
     string data3 = "test3"; 
     string data4 = "test4"; 


     foreach (string s in mylist) 
     { 
      bool found = false; 

      if(data1.Equals(s) || data1.Equals("Do not Search")) 
      { 
       found = true; 
      } 

      if (data2.Equals(s) || data1.Equals("Do not Search")) 
      { 
       found = true; 
      } 

      if (data3.Equals(s) || data1.Equals("Do not Search")) 
      { 
       found = true; 
      } 

      if (data4.Equals(s) || data1.Equals("Do not Search")) 
      { 
       found = true; 
      } 


     } 

或者這樣的一和條件

 List<string> mylist = new List<string>(); 
     string data1 = "test1"; 
     string data2 = "test2"; 
     string data3 = "test3"; 
     string data4 = "test4"; 


     foreach (string s in mylist) 
     { 
      bool found = false; 
      bool notfound = false; 

      if(data1.Equals(s) || data1.Equals("Do not Search")) 
      { 
       found = true; 
      } 
      else 
      { 
       notfound = true; 
      } 

      if (data2.Equals(s) || data1.Equals("Do not Search")) 
      { 
       found = true; 
      } 
      else 
      { 
       notfound = true; 
      } 
      if (data3.Equals(s) || data1.Equals("Do not Search")) 
      { 
       found = true; 
      } 
      else 
      { 
       notfound = true; 
      } 
      if (data4.Equals(s) || data1.Equals("Do not Search")) 
      { 
       found = true; 
      } 
      else 
      { 
       notfound = true; 
      } 

      // Force all to match 
      if (notfound) 
       return null; 
     } 

我更傾向於將這樣的事情,雖然在那裏你可以利用搜索功能,你需要做什麼.....

List<string> mylist = new List<string>(); 

     List<string> mysearches = new List<string>(); 

     string data1 = "test1"; 
     string data2 = "test2"; 
     string data3 = "test3"; 
     string data4 = "test4"; 

     if(data1 != "Do not Search") 
      mysearches.Add(data1); 
     if (data2 != "Do not Search") 
      mysearches.Add(data2); 
     if (data3 != "Do not Search") 
      mysearches.Add(data3); 
     if (data4 != "Do not Search") 
      mysearches.Add(data4); 
     bool found = false; 
     bool andconditionmatch = true; 

     foreach (string s in mylist) 
     { 
      if (mysearches.Contains(s)) 
      { 
       found = true; 
      } 
      else 
      { 
       andconditionmatch = false; 
      } 
     } 
+0

請注意,如果您要求所有(和)匹配或只有一個(或),您需要調整的最後一個選項。 - 修改顯示的答案 – tsells 2012-04-16 23:12:57

0

將所有可能性都放在哈希集中。

Hashset with all possibilities. 

foreach(item in selectedItems) //you may not need this if you dont have to perform action forall selected items. 
{ 
    if (Hashset contains item) 
     //do stuff. 
} 
0

我會移動匹配的類內,而不是與7個獨立的可能值處理相匹配,使他們一個類實例調用criteria。請參見下面的示例代碼:

public enum States 
{ 
    None, 
    Tenesee, 
    Georgia, 
    Colorado, 
    Florida 
} 
class Item 
{ 
    public States State { get; set; } 
    public string Name { get; set; } 
    public int ID { get; set; } 

    public bool IsMatch(Item criteria) 
    { 
     bool match = true; 
     if (criteria.State != States.None) match &= criteria.State == State; 
     if (!string.IsNullOrEmpty(criteria.Name)) match &= criteria.Name.Equals(Name); 
     if (criteria.ID > 0) match &= criteria.ID == ID; 
     return match; 
    } 
    public override string ToString() 
    { 
     return string.Format("ID={0}, Name={1}, State={2}", ID.ToString(), Name, State.ToString()); 
    } 
} 
class Program 
{ 

    static void Main(string[] args) 
    { 

     List<Item> list = new List<Item>(); 
     list.Add(new Item() { ID = 10016, Name = "Julia", State = States.Georgia }); 
     list.Add(new Item() { ID = 10017, Name = "Scott", State = States.Colorado }); 
     list.Add(new Item() { ID = 10018, Name = "Samantha", State = States.Tenesee }); 
     list.Add(new Item() { ID = 10019, Name = "Julia", State = States.Florida }); 


     Item criteria = new Item() 
     { 
      State = States.Tenesee, 
      ID = 10018 
     }; 
     List<Item> selection = list.FindAll((item) => item.IsMatch(criteria)); 

     foreach (var item in selection) 
     { 
      Console.WriteLine("{0}", item); 
     } 

    } 
} 

有了結果 ID=10018, Name=Samantha, State=Tenesee

所以你建立一個標準,例如Item並比較匹配,如果屬性是明確界定。遍歷所有項目並選擇符合條件的項目。顯然你必須擴展.IsMatch()所有7個屬性。