我有以下代码
Regex R = new Regex("my regex");
var q = from c in db.tble1
where R.IsMatch(c.text)
select c;
在调试时我在 q 结果中看到了这条消息
Method 'Boolean IsMatch(System.String)' has no supported translation to 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;
或者,您可以执行内存中正则表达式比较。您可以通过在使用 Regex 之前调用 ToList() 来执行此操作:
Regex R = new Regex("my regex");
var q = from c in db.tble1.ToList()
where R.IsMatch(c.text)
select c;
