Linq Regex用于全字搜索

我最近找不到合适的答案。

Linq不支持正则表达式,尝试提取方法并不能超越框架。 如何在linq中的句子列表上进行整个工作匹配。

我需要\ b的原因是因为它可能是字符串的开头或结尾,或者有逗号,短划线或其他类似的分隔符。

private bool isMatch(string searchText, string text) { return Regex.IsMatch(searchText, "\\b"+text+"\\b", RegexOptions.IgnoreCase | RegexOptions.Compiled); } result p = itemsRep .Where(fullText=> isMatch(searchText, fullText)) .FirstOrDefault(); 

我想你所说的是Linq-to-SQL / Linq-To-Entities在表达式中不支持Regex

尝试在.Where()之前添加.AsEnumerable() .Where()

然后.Where()方法将枚举任何已翻译查询的结果,而不是尝试将.Where()表达式转换为SQL。

像这样:

 result p = itemsRep .AsEnumerable() .Where(fullText => isMatch(searchText, fullText)) .FirstOrDefault();