我有以下代码
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转换,
但如何解决这个问题
Regex没有受支持的SQL翻译.错误说明了一切.您不能在LINQ to SQL中使用正则表达式.
请尝试使用like或substring比较:
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)