【发布时间】:2012-08-09 19:08:21
【问题描述】:
经过数小时的尝试和搜索,我想现在是时候与您分享我的问题了。
问题定义: 我有一个 KeyValuePairs 字典(名为 filterPool),其中包括一个整数(PropertyID)和一个字符串(McValue )。我要做的是根据这些 KeyValuePairs 过滤产品并将它们作为 DataTable/List 返回。 您可以将其视为将动态“Where ... And ..”子句构建为 SQL。
这是我正在使用的代码:
foreach (KeyValuePair<int, string> filter in filterPool)
{
products = products.Where(i => i.PROPERTYID == filter.Key && i.MCVALUE.Equals(filter.Value));
}
return products.ToDataTable();
问题是上面的 foreach 循环似乎只工作一次,对于字典中可用的最新 KeyValuePair。
据我在 Stackoverflow 上找到的,最接近我的问题的解决方案是:this one, also using a Dictionary of values for filtering
一定有办法使用Dictionary和LINQ实现过滤的目的;或者有一件我想念/忽略的大事。
希望给出的问题对所有人来说都足够清楚, 谢谢 ^^
【问题讨论】:
标签: asp.net linq dictionary code-behind dynamicquery