我需要创建一个表达式,使其像:
Expression<Func<MyDataSet, bool>> searchfilter = null;
string[] strsearchvalues = Searchvalues.Split(',');
searchfilter = p => p.searchcolumn.contains(strsearchvalues[0]) && p.searchcolumn.contains(strsearchvalues[1]) && ....
我试过这个,但它不起作用:
Expression<Func<MyDataSet, bool>> columnfilter = null;
foreach(var s in strsearchvalues)
{
columnfilter = p => p.searchcolumn.contains(s);
searchfilter = columnfilter.And(columnfilter);
}
如果搜索值是“bob Smith”,我想结果返回该列包含bob和smith,如“boby smither”或“smither boby”。使用上面的代码,它返回'boby something'或'史密斯的东西'。
如何创建带循环的Expression来完成它?
谢谢!
答案 0 :(得分:0)
尝试这样做:
Expression<Func<MyDataSet, bool>> columnfilter = null;
foreach(var s in strsearchvalues)
{
columnfilter = columnfilter == null ? p => p.searchcolumn.contains(s) : columnfilter.And(p => p.searchcolumn.contains(s)) ;
}
searchfilter = columnfilter;
希望这对你有帮助......