在 MVC4 中,我试图将模型的所有元素添加到数组中,以便获取所有非空值的计数。
例如:型号
public bool? Intubated_1 {get; set; }
public bool? Intubated_2 {get; set; }
public bool? Intubated_3 {get; set; }
public bool? Intubated_4 {get; set; }
public bool? Intubated_5 {get; set; }
public bool? some other fields
ETC...
有没有办法将所有 Intubated_x 值放入数组中以获取所有非空值的计数,或者我是否必须将所有 32 个值放入语句中并单独检查并添加到数组中?
编辑: 非常感谢所有的解决方案。我拿了一些碎片并进行了一些修改以适合我正在寻找的东西。我还有其他布尔值,所以我可以遍历所有布尔值,所以关键是建议的 .Name.StartsWith 。
List<bool?> intubatedList = new List<bool?>();
foreach (var p in adm.GetType().GetProperties())
{
if (p.Name.StartsWith("Intubated") && p.GetValue(adm) != null)
intubatedList.Add((bool?)p.GetValue(adm));
}
if (intubatedList.Count == adm.LOS)
return true;