[英]Query a collection using PropertyInfo object in LINQ
I have a method with a signature like this 我有这样的签名方法
void RefreshMethod<T>(IEnumerable<T> lst, string propertyName) where T:class
{
Type type = typeof(T);
PropertyInfo property = type.GetProperties().Single(u => u.Name == primaryKeyProperty);
//query goes here
}
Now i want to query that collection for getting all the values whose 现在我想查询该集合以获取其值的所有值
propertyName < 0 propertyName <0
In a simple scenario it would be as easy as this 在一个简单的场景中,就像这样简单
lst.where(u=>u.ID<0)
But here i don't have that ID property but have corresponding "PropertyInfo" object. 但是这里我没有那个ID属性,但是有相应的“ PropertyInfo”对象。
How should i acheive this. 我应该如何做到这一点。
kindly guide 友善的指导
You can lookup the property-value using property.GetValue(anObjectOfTypeT, null)
. 您可以使用property.GetValue(anObjectOfTypeT, null)
查找属性值。
So something like: 所以像这样:
var refreshedList = lst.Where(l => ((int)(property.GetValue(l, null)) < 0).ToList();
This assumes the property will always be of type int though. 假设该属性始终为int类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.