[英]Query a collection using PropertyInfo object in LINQ
我有這樣的簽名方法
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
}
現在我想查詢該集合以獲取其值的所有值
propertyName <0
在一個簡單的場景中,就像這樣簡單
lst.where(u=>u.ID<0)
但是這里我沒有那個ID屬性,但是有相應的“ PropertyInfo”對象。
我應該如何做到這一點。
友善的指導
您可以使用property.GetValue(anObjectOfTypeT, null)
查找屬性值。
所以像這樣:
var refreshedList = lst.Where(l => ((int)(property.GetValue(l, null)) < 0).ToList();
假設該屬性始終為int類型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.