[英]How do i group a list of dynamic objects using linq given that the property names are dynamic?
I am trying to group the results from a dynamic object list. 我正在尝试将动态对象列表中的结果分组。 I tried the following code 我尝试了以下代码
var results = ((IEnumerable)data).Cast<dynamic>().Where(x => x.dynamicProperyname));
The property name itself is also dynamic and hence i cannot use it in my query as shown above. 属性名称本身也是动态的,因此如上所述,我无法在查询中使用它。 Any suggestions on how to fix this? 对于如何解决这个问题,有任何的建议吗?
This should do the trick - 这应该可以解决问题-
var propertyName = "dynamicProperyname";
var results = ((IEnumerable)data).Cast<dynamic>()
.Where(x => x.GetType().GetProperty(propertyName)?.GetValue(x, null) ?? false);
Above code use reflection to get value of property. 上面的代码使用反射来获取属性的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.