【发布时间】:2016-05-04 22:31:49
【问题描述】:
我有一个列表,我想根据 KeyValuePairs 列表进行过滤。 KeyValuePair 中的所有键都存在于对象中。
假设我有一个此类对象的列表:
public class filters
{
public string Name { get; set; }
public string Age { get; set; }
public string Country { get; set; }
}
我有一个 KeyValuePair:
Key: "Name", Value: "test"
Key: "Country", Value: "SE"
是否可以从 KeyValuePair 生成某种可用作list.Where(predicate) 的 LINQ 谓词,并且该谓词与我写的 list.Where(c => c.Name == "test" && c.Country == "SE") 相同?
或者我应该如何处理这个问题?
【问题讨论】:
-
你试过反射吗?
-
不确定如何使用反射
-
var keySet = new Dictionary<string, string> { { "Name", "test" } }; Predicate<filters> condition = (c) => c.Name == keySet["Name"] && c.Country == keySet["Country"]; list.Where(condition);这是你感兴趣的吗?