Filtering collection with LINQ

后端 未结 4 1193
醉话见心
醉话见心 2021-02-02 02:19

Let\'s say we have a collection of Person objects

class Person 
{
     public string PersonName {get;set;}
     public string PersonAddress {get;set;}    
}
         


        
4条回答
  •  有刺的猬
    2021-02-02 03:01

    You can do it via reflection:

    IQueryable filteredPersons = personsList.AsQueryable();
    Type personType = typeof(Person);
    foreach(Filter filter in userFilters) {
        filteredPersons = filteredPersons.Where(p => (string)personType.InvokeMember(filter.FieldName, BindingFlags.GetProperty, null, p, null) == filter.FilterString);
    }
    

    (not compiled, but this should be along the right track)

提交回复
热议问题