對於我正在處理的ASP.NET MVC項目,我需要檢查LINQ-To-SQL類中的位變量是否爲真。到目前爲止,檢查每個變量是否爲真或假後,我便推字段的值轉換成一個列表,並返回它像這樣:C#列表/檢查對象變量?
public List<String> GetVarList() {
List<String> list = new List<String>();
if (fields.SearchBar) {
list.Add("SearchBar");
}
if (fields.SomeField) {
list.Add("SomeField");
}
return list;
}
這對我來說,似乎並沒有被最快或最簡單的方法來做到這一點。
我想知道它有可能以某種方式通過for或foreach循環遍歷它們來從一個字符串數組中動態檢查變量的值。例如:
public List<String> GetVarList() {
String[] array = {"SearchBar", "SomeField"};
List<String> list = new List<String>();
foreach (String field in array) {
// Check whether or not the value is true dynamically through the array
}
return list;
}
感謝您的任何建議!
您想從最終用戶的角度來完成什麼? – 2010-09-07 20:06:11