Regex没有使用Linq到Sql

Sta*_*tar 1 c# regex linq

我有以下代码

Regex R = new Regex("my regex");
var q = from c in db.tble1
        where R.IsMatch(c.text)
        select c;
Run Code Online (Sandbox Code Playgroud)

而在调试时我在q结果中看到了这条消息

方法'Boolean IsMatch(System.String)'没有支持的SQL转换."} System.SystemException {System.NotSupportedException}

那我做错了什么?

编辑:我了解到该方法没有支持的SQL转换,
但如何解决这个问题

jru*_*ell 9

Regex没有受支持的SQL翻译.错误说明了一切.您不能在LINQ to SQL中使用正则表达式.

请尝试使用likesubstring比较:

var q = from c in db.tble1
        where c.text.StartsWith("x") && c.text.Substring(2, 1) == "y"
        select c;
Run Code Online (Sandbox Code Playgroud)

或者,您可以执行内存正则表达式比较.您可以ToList()在使用之前通过调用来执行此操作Regex:

Regex R = new Regex("my regex");
var q = from c in db.tble1.ToList()
        where R.IsMatch(c.text)
        select c;
Run Code Online (Sandbox Code Playgroud)

  • @Star是的,但又一次:TSQL**不支持**正则表达式.剩下的唯一选择是在服务器上编写和安装的SQL-CLR函数,它不会有效(它会进行表扫描) (2认同)