问题描述
我找不到关于这个的最新答案.
I'm unable to find a recent decent answer on this.
Linq不支持正则表达式,并且尝试提取方法并不比框架更加聪明.如何在linq中的句子列表上完成整个工作.
Linq does not support regex, and trying to extract a method out does not outsmart the framwork. How do I do a whole work match on a list of sentences in linq.
我需要\ b的原因是因为它可能是字符串的开头或结尾,或者有逗号,破折号或其他类似的分隔符.
The reason I need the \b is because it could be start or end of string, or have a comma, dash, or other similar separators.
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
.
I think what you're saying is that Linq-to-SQL/Linq-To-Entities does not support Regex
in expressions.
尝试在.Where()
之前添加.AsEnumerable()
.
然后,.Where()
方法将枚举任何已翻译查询的结果,而不是尝试将您的.Where()
表达式转换为SQL.
The .Where()
method will then enumerate over the results of any translated query, rather than try to convert your .Where()
expression to SQL.
赞:
result p = itemsRep
.AsEnumerable()
.Where(fullText => isMatch(searchText, fullText))
.FirstOrDefault();
这篇关于Linq正则表达式用于全词搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!