【发布时间】:2018-03-09 02:39:32
【问题描述】:
我想构建一个函数,用户可以在其中搜索列表中的某些属性是否包含值
假设我们将拥有 List,Company 将被定义为具有以下属性的类:
public class Company
{
public int Id { get; set; }
public string Name { get; set; }
public string CompanyAddress1 { get; set; }
public string CompanyPostCode { get; set; }
public string CompanyCity { get; set; }
public string CompanyCounty { get; set; }
}
现在 - 理想情况下,我希望使用此参数来实现功能
List<Company> FilterCompanies(List<Company> unfilteredList, string fieldToQueryOn, string query)
{
// linq version what ideally would like to archeve
return unfilteredList.Where(x => x."fieldToQueryOn".ToString().ToLower().Contains(query.ToLower())).ToList();
}
然后打电话:
var variable = FilterCompanies(NotNullFilledUnfilteredList, "CompanyCity", "New York")
我尝试按照docs.microsoft.com 上的教程进行操作,这很简单,但我不知道如何通过对 Type 的反射来扩展该解决方案并将其用于表达式树。
【问题讨论】:
标签: c# linq expression-trees