2016-09-05 60 views
6

在我的表單中我有四個RadioButton S,根據用戶的選擇,該代碼被執行:處理重複邏輯冗餘代碼的最佳方法是什麼?

private void button1_Click(object sender, EventArgs e) 
     { 
      listBox1.Items.Clear(); 
      if (radioButtonName.Checked) 
      { 
       var Qr = from n in mylist where n.Name == textBoxSearch.Text select new { n.Name, n.Age, n.Occu, n.Gender }; 
       foreach (var item in Qr) 
       { 
        listBox1.Items.Add("Name: " + item.Name + " " + " Age: " + item.Age + " " + " Occupation: " + item.Occu + " " + " Gender: " + item.Gender); 
       } 
      } 
      if (radioButtonAge.Checked) 
      { 
       var Qr = from n in mylist where n.Age == textBoxSearch.Text select new { n.Name, n.Age, n.Occu, n.Gender }; 
       foreach (var item in Qr) 
       { 
        listBox1.Items.Add("Name: " + item.Name + " " + " Age: " + item.Age + " " + " Occupation: " + item.Occu + " " + " Gender: " + item.Gender); 
       } 

      } 
      if (radioButtonGender.Checked) 
      { 
       var Qr = from n in mylist where n.Gender == textBoxSearch.Text select new { n.Name, n.Age, n.Occu, n.Gender }; 
       foreach (var item in Qr) 
       { 
        listBox1.Items.Add("Name: " + item.Name + " " + " Age: " + item.Age + " " + " Occupation: " + item.Occu + " " + " Gender: " + item.Gender); 
       } 
      } 
      if (radioButtonOccupation.Checked) 
      { 
       var Qr = from n in mylist where n.Occu == textBoxSearch.Text select new { n.Name, n.Age, n.Occu, n.Gender }; 
       foreach (var item in Qr) 
       { 
        listBox1.Items.Add("Name: " + item.Name + " " + " Age: " + item.Age + " " + " Occupation: " + item.Occu + " " + " Gender: " + item.Gender); 
       } 

      } 

     } 

的代碼似乎很冗餘和重複,但我也不能找到一種方法來處理所有4個單選按鈕在只有一個變量鏈接到用戶選擇的單行中。 myList是I類創建具有4種string性質(NameAgeGenderOccu)的List

回答

8

唯一的區別是在濾波器where)中的所有其它可被組合:

private void button1_Click(object sender, EventArgs e) { 
    var lines = mylist 
    .Where(item => radioButtonName.Checked && item.Name == textBoxSearch.Text || 
        radioButtonAge.Checked && item.Age == textBoxSearch.Text || 
        radioButtonGender.Checked && item.Gender == textBoxSearch.Text || 
        radioButtonOccupation.Checked && item.Occu == textBoxSearch.Text) 
    .Select(item => string.Format("Name: {0} Age: {1} Occupation: {2} Gender: {3}", 
            item.Name, item.Age, item.Occu, item.Gender)); 

    listBox1.Items.Clear(); 

    foreach (string line in lines) 
    listBox1.Items.Add(line); 
} 
+1

快速的問題應該在單選按鈕檢查和文本比較用括號括起來或者它沒有關係?例如'(radioButtonName.Checked && item.Name == textBoxSearch.Text)|| (radioButtonAge.Checked && item.Age == textBoxSearch.Text)...' – Nkosi

+1

@Nkosi:在上下文中沒有關係;但是,如果帶有父項的版本更易於讀取*,則可以添加'(...)' –

+0

@Nkosi請參閱[此答案](http://stackoverflow.com/a/1196738/5555803)。首先得到評估 –

1

您可以使用字典一次性將RadioButton映射到其Filter。假設MyClass在你的列表中的對象類型:

private void button1_Click(object sender, EventArgs e) 
{ 
    var mapping = new Dictionary<RadioButton, Func<MyClass, bool>>() 
    { 
     { radioButtonName , x => x.Name == textBoxSearch.Text }, 
     { radioButtonAge, x => x.Age == textBoxSearch.Text }, 
     { radioButtonGender, x => x.Gender == textBoxSearch.Text}, 
     { radioButtonOccupation, x => x.Occu == textBoxSearch.Text} 
    }; 

    foreach(var map in mapping.Where(x=> x.Key.Checked)) 
    { 
     var Qr = mylist.Where(map.Value).Select(n=> new {n.Name, n.Age, n.Occu, n.Gender}); 
     foreach (var item in Qr) 
     { 
      listBox1.Items.Add("Name: " + item.Name + " " + " Age: " + item.Age + " " 
          + " Occupation: " + item.Occu + " " + " Gender: " 
          + item.Gender); 
     } 
    } 

} 

這樣你可以很容易地使用字典中的一個簡單的線插入新的單選按鈕。

0

您可以首先生成一個單選按鈕的匿名列表,並在其中進行預測然後遍歷它(在這種情況下MyItem是您的列表包含的樣本/佔位符,因爲我不知道實際的類名稱):

private void button1_Click(object sender, EventArgs e) 
    { 
     // Generate anonymous list of objects that are different 
     var radios = new[] 
     { 
      new { RadioButton = radioButtonName, CallBack = new Func<MyItem, bool>(x => x.Name == textBoxSearch.Text) }, 
      new { RadioButton = radioButtonAge, CallBack = new Func<MyItem, bool>(x => x.Age == textBoxSearch.Text) }, 
      new { RadioButton = radioButtonGender, CallBack = new Func<MyItem, bool>(x => x.Occu == textBoxSearch.Text) }, 
      new { RadioButton = radioButtonOccupation, CallBack = new Func<MyItem, bool>(x => x.Gender == textBoxSearch.Text) }, 
     }; 

     // Iterate through list and add items to ListBox1, if RadioButtton is checked 
     listBox1.Items.Clear(); 
     foreach (var radio in radios) 
     { 
      if (!radio.RadioButton.Checked) 
      { 
       continue; 
      } 
      var Qr = mylist.Where(radio.CallBack).Select(n => new { n.Name, n.Age, n.Occu, n.Gender }); 
      foreach (var item in Qr) 
      { 
       listBox1.Items.Add($"Name: {item.Name}  Age: {item.Age}  Occupation: {item.Occu}  Gender: {item.Gender}"); 
      } 
     } 
    } 
7

包裹一切都在一個函數像這樣的:

public void foo(RadioButton radioButton, Expression<Func<MyItem, bool>> expression) 
    { 
     if (radioButton.Checked) 
     { 
      var Qr = mylist.AsQueryable().Where(expression).Select(x => String.Format("Name: {0}, Age: {1}, Occ: {2}, Gender: {3}", x.Name, x.Age, x.Occu, x.Gender)).ToList(); 

      foreach (var item in Qr) 
      { 
       listBox1.Items.Add(item); 
      } 
     } 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     listBox1.Items.Clear(); 
     foo(radioButtonName, c => c.Gender == textBoxSearch.Text); 
     foo(radioButtonAge, c => c.Age == textBoxSearch.Text); 
     foo(radioButtonGender, c => c.Gender == textBoxSearch.Text); 
     foo(radioButtonOccupation, c => c.Occu == textBoxSearch.Text); 
    } 



public class MyItem 
    { 
     public String Occu { get; set; } 

     public String Age { get; set; } 
     public String Name { get; set; } 
     public String Gender { get; set; } 

    } 
+0

這隻適用於一個屬性'Occu'! –

+0

@MohamedAhmed爲什麼投了票?檢查我的更新... – raven

相關問題