我的课如下:
public class Entity
{
public string Name {get;set;}
public int ID {get;set;}
public string Desc {get;set;}
}
我的
List
为Entity
:List<Entity> ent = new List<Entity>()
{
new Entity {Name ="A", ID =1},
new Entity {Name ="B", ID = 2}
};
在列表中,我们可以看到每个对象的“ Desc”值都是空白。有没有办法找出Name属性,对于List中的所有对象,其值为空。
在此示例中,输出为“ Desc”,而不使用for循环,循环对象并保留标志?
最佳答案
使用LINQ:
var Properties = typeof(Entity).GetProperties()
.Where(propertyInfo => ent.All(entity => propertyInfo.GetValue(entity, null) == null))
.Select(c=>c.Name);
Properties
是IEnumerable
的String
,是IEnumerable
的Properties
的Name
,对于每个对象,其值均为null。关于c# - 从对象列表中删除列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40152916/