假设我有一个这样的类:如何过滤在LINQ有不同的参数列表
public class SampleClass
{
public string SampleProperty1 { get; set; }
public string SampleProperty2 { get; set; }
public string SampleProperty3 { get; set; }
public string SampleProperty4 { get; set; }
public string SampleProperty5 { get; set; }
}
而且我有一个这样的名单:
List<SampleClass> sampleList = new List<SampleClass>();
我要过滤通过名单SampleProperty1
和SampleProperty5
。下次我会用SampleProperty3
或SampleProperty2
。我的意思是用户可以过滤他想要的任何属性。
我该如何实现这种灵活性?
我不想编写if语句和属性一样多,因为属性的实际数量更多。
有没有一个聪明的方法来做到这一点?
谢谢。
您需要根据用户选定的过滤器“生成”谓词集合,然后才能将这些谓词应用到列表中。如何根据字段类型和过滤器类型(相等,包含,更大...)生成它们 – Fabio
[LINQ中动态WHERE子句]的可能重复(http://stackoverflow.com/questions/848415/dynamic- where-clause-in-linq) – Pikoh
还有更多... – Pikoh