简体   繁体   English

Linq正则表达式用于全词搜索

[英]Linq Regex for whole word search

I'm unable to find a recent decent answer on this. 我无法找到关于此的最新答案。

Linq does not support regex, and trying to extract a method out does not outsmart the framwork. Linq不支持正则表达式,并且尝试提取方法并不比框架更加聪明。 How do I do a whole work match on a list of sentences in linq. 如何在linq中的句子列表上进行完整的工作匹配。

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. 我需要\\ 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();

I think what you're saying is that Linq-to-SQL/Linq-To-Entities does not support Regex in expressions. 我认为您是说Linq-to-SQL / Linq-To-Entities不支持Regex中的Regex

Try adding .AsEnumerable() before your .Where() . 尝试在.Where()之前添加.AsEnumerable() .Where()

The .Where() method will then enumerate over the results of any translated query, rather than try to convert your .Where() expression to SQL. 然后, .Where()方法将枚举任何已翻译查询的结果,而不是尝试将.Where()表达式转换为SQL。

Like this: 像这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM