Query a collection using PropertyInfo object in LINQ

前端 未结 1 1982
悲哀的现实
悲哀的现实 2020-12-18 07:57

I have a method with a signature like this

void RefreshMethod(IEnumerable lst, string propertyName) where T:class
{
   Type type = typeof(T         


        
相关标签:
1条回答
  • 2020-12-18 08:24

    You can lookup the property-value using 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.

    0 讨论(0)
提交回复
热议问题