【发布时间】:2014-01-16 23:53:27
【问题描述】:
我想过滤我的列表以用于报告目的,但我想让它尽可能动态,以便用户可以过滤 1 列或更多列。
我的想法是创建一个字典,然后将其传递给一个过滤掉相关记录的方法,但我现在对如何执行“动态”部分感到困惑。
这是我目前的代码到目前为止的样子,但它不起作用。
filters = {CourseId,2},{CourseDescription,Maths}
public IQueryable<Course> Filter(Dictionary<string,string> filters)
{
var a = from s in context.Courses
select s;
foreach (var filter in filters)
{
if(!string.IsNullOrEmpty(filter.Value))
{
a = a.Where(s => s./*filter.Key*/.ToUpper().Contains(/*filter.value*/.ToUpper()));
}
}
}
请有人协助并指出正确的方向,以便我可以正常工作。
谢谢。
【问题讨论】:
-
您的
filters定义与Dictionary<string, string>声明不匹配。您的代码中没有任何string... -
也许这个问题与你想要做什么有关? stackoverflow.com/questions/848415/linq-dynamic-where-clause
-
这个是微软人写的,可以通过字符串的参数来构建IQueryable,其实和Visual Studio一起在Samples中,即Dynamic.cs.weblogs.asp.net/scottgu/archive/2008/01/07/…