两个集合之间数据过滤linq
//以下为前台多字段循环匹配
if (!input.Filter.IsNullOrEmpty())
{
var keysList = input.Filter.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
if (keysList.Length > 0)
{
Expression<Func<Content, bool>> pr = null;
for (int i = 0; i < 3 && i < keysList.Length; i++)
{
var item = keysList[i];
Expression<Func<Content, bool>> filter = t => t.Title.Contains(item);
pr = (pr == null ? filter : pr.And(filter));
}
Expression<Func<Content, bool>> pr2 = null;
for (int i = 0; i < 3 && i < keysList.Length; i++)
{
var item = keysList[i];
Expression<Func<Content, bool>> filter = t => t.Body.Contains(item);
pr2 = (pr2 == null ? filter : pr2.And(filter));
}
q = q.Where(pr.Or(pr2));
}
}
本文来自博客园,作者:WantRemake,转载请注明原文链接:https://www.cnblogs.com/SmallChen/p/15020838.html