Fork me on GitHub

两个集合之间数据过滤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));
}
}

posted @ 2021-07-16 16:44  WantRemake  阅读(166)  评论(0编辑  收藏  举报