C# 将相似文本模型中的数据元素添加到数组中,以获得总非空计数MVC4

C# 将相似文本模型中的数据元素添加到数组中,以获得总非空计数MVC4,c#,asp.net-mvc-4,C#,Asp.net Mvc 4,在MVC4中,我试图将模型的所有元素添加到一个数组中,以便获得所有非空值的计数 例如: 模型 等等 是否有办法将所有插入的_x值放入一个数组中,以获得所有非空值的计数,或者我必须将所有32个值放入语句中,并分别进行检查和添加到数组中 编辑: 非常感谢所有的解决方案。我拿了几件,做了一些修改,以符合我的要求。我还有其他布尔值,所以我可以遍历所有布尔值,所以关键是建议使用的.Name.starts List<bool?> intubatedList = new List<bool?

在MVC4中,我试图将模型的所有元素添加到一个数组中,以便获得所有非空值的计数

例如: 模型

等等

是否有办法将所有插入的_x值放入一个数组中,以获得所有非空值的计数,或者我必须将所有32个值放入语句中,并分别进行检查和添加到数组中

编辑: 非常感谢所有的解决方案。我拿了几件,做了一些修改,以符合我的要求。我还有其他布尔值,所以我可以遍历所有布尔值,所以关键是建议使用的.Name.starts

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;
List intubatedList=new List();
foreach(adm.GetType().GetProperties()中的var p)
{
if(p.Name.StartsWith(“插管”)和p.GetValue(adm)!=null)
插管列表添加((bool?)p.GetValue(adm));
}
如果(插管列表计数==行政服务水平)
返回true;

实现这一点的唯一方法是通过反射。使用以下函数可以枚举所有属性,获取它们的值并将它们添加到列表中:

List<bool?> myList = new List<bool?>();
foreach (var p in obj.GetType().GetProperties())
{
    mylist.Add((bool?)p.GetValue(obj, null));
}
List myList=new List();
foreach(obj.GetType().GetProperties()中的var p)
{
mylist.Add((bool?)p.GetValue(obj,null));
}

当然,如果类中有其他属性,则在将其添加到数组之前,需要检查它是否是要检查的属性之一(例如
p.Name.StartsWith(“Intuided”)

唯一的方法是通过反射。使用以下函数可以枚举所有属性,获取它们的值并将它们添加到列表中:

List<bool?> myList = new List<bool?>();
foreach (var p in obj.GetType().GetProperties())
{
    mylist.Add((bool?)p.GetValue(obj, null));
}
List myList=new List();
foreach(obj.GetType().GetProperties()中的var p)
{
mylist.Add((bool?)p.GetValue(obj,null));
}
当然,如果类中有其他属性,则在将其添加到数组之前,需要检查它是否是要检查的属性之一(例如
p.Name.StartsWith(“Intuided”)

检查以下内容:

    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 List<bool?> NullableBoolPropertiesList()
    {
        List<bool?> properties = new List<bool?>();

        Type type = this.GetType();

        foreach (System.Reflection.PropertyInfo property in type.GetProperties())
        {
            if (property.PropertyType == typeof(bool?))
            {
                bool? value = property.GetValue(this) as bool?;

                properties.Add(value);
            }
        }

        return properties;
    }
公共图书馆?插管的_1{get;set;} 公共场所?插管式_2{get;set;} 公共场所?插管式_3{get;set;} 公共场所?插入式_4{get;set;} 公共场所?插管的_5{get;set;} 公共列表NullableBoolPropertiesList() { 列表属性=新列表(); Type Type=this.GetType(); foreach(type.GetProperties()中的System.Reflection.PropertyInfo属性) { if(property.PropertyType==typeof(bool?) { bool?value=property.GetValue(this)作为bool?; 增加(价值); } } 归还财产; } 用法:

        List<bool?> nullableBools = yourObject.NullableBoolPropertiesList();

        int howManyAreSet = nullableBools.Where(x => x.HasValue).Count();
        int howManyAreTrue = nullableBools.Where(x => x.HasValue && x.Value).Count();
List nullableBools=yourObject.NullableBoolPropertiesList();
int howmanyreset=nullableBools.Where(x=>x.HasValue.Count();
int howmanyretrue=nullableBools.Where(x=>x.HasValue&&x.Value).Count();
查看以下内容:

    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 List<bool?> NullableBoolPropertiesList()
    {
        List<bool?> properties = new List<bool?>();

        Type type = this.GetType();

        foreach (System.Reflection.PropertyInfo property in type.GetProperties())
        {
            if (property.PropertyType == typeof(bool?))
            {
                bool? value = property.GetValue(this) as bool?;

                properties.Add(value);
            }
        }

        return properties;
    }
公共图书馆?插管的_1{get;set;} 公共场所?插管式_2{get;set;} 公共场所?插管式_3{get;set;} 公共场所?插入式_4{get;set;} 公共场所?插管的_5{get;set;} 公共列表NullableBoolPropertiesList() { 列表属性=新列表(); Type Type=this.GetType(); foreach(type.GetProperties()中的System.Reflection.PropertyInfo属性) { if(property.PropertyType==typeof(bool?) { bool?value=property.GetValue(this)作为bool?; 增加(价值); } } 归还财产; } 用法:

        List<bool?> nullableBools = yourObject.NullableBoolPropertiesList();

        int howManyAreSet = nullableBools.Where(x => x.HasValue).Count();
        int howManyAreTrue = nullableBools.Where(x => x.HasValue && x.Value).Count();
List nullableBools=yourObject.NullableBoolPropertiesList();
int howmanyreset=nullableBools.Where(x=>x.HasValue.Count();
int howmanyretrue=nullableBools.Where(x=>x.HasValue&&x.Value).Count();

也许可以尝试一些更圆滑的东西:

private readonly bool?[] _boolArray = new bool?[5];

// Your properties
public bool? Intubated_1
{
    get
    {
        return _boolArray[0];
    }
    set
    {
        _boolArray[0] = value;
    }
}

public bool? Intubated_2
{
    get
    {
        return _boolArray[1];
    }
    set
    {
        _boolArray[1] = value;
    }
}


// Count
public int CountNonNull
{
    get
    {
        return _boolArray.Count(i => i != null);
    }
}

也许可以试试更圆滑的东西:

private readonly bool?[] _boolArray = new bool?[5];

// Your properties
public bool? Intubated_1
{
    get
    {
        return _boolArray[0];
    }
    set
    {
        _boolArray[0] = value;
    }
}

public bool? Intubated_2
{
    get
    {
        return _boolArray[1];
    }
    set
    {
        _boolArray[1] = value;
    }
}


// Count
public int CountNonNull
{
    get
    {
        return _boolArray.Count(i => i != null);
    }
}

这是你的视图模型,还是数据库中的某个实体?伙计,到目前为止有什么反馈吗?看起来您已经得到了所需的答案。@Johnny5这是在数据库中的我的实体中。@AndreCalil我正在尝试下面的选项。我不得不离开一会儿。这是你的视图模型,还是数据库中的某个实体?伙计,到目前为止有什么反馈吗?看起来您已经得到了所需的答案。@Johnny5这是在数据库中的我的实体中。@AndreCalil我正在尝试下面的选项。我不得不离开一会儿。