在 linq 中获取某个属性名称的所有值?

yal*_*cat 1 c# linq

有没有办法在 linq 中获取某个键的所有值?

这是我的收藏

[PID、Pname、环境名称]

所以我有一个这种类型的集合,寻找一种更通用的方法,我可以在其中检查属性名称并进行选择。就像是,

myCollection.SelectMany(item => item).Where(item==propertyName)

我怎样才能做到这一点?

Ric*_*der 5

要获取一组named属性值,请尝试

var pName = "PId";
var values = myCollection
  .Select(x => x.GetType().GetProperty(pName).GetValue(x, null))
  .ToArray();
Run Code Online (Sandbox Code Playgroud)