1

我有以下代码

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

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

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

所以我做错了什么?

编辑:我了解到该方法不支持对 SQL 的翻译,
但如何解决这个问题

4

1 回答 1

9

正则表达式不支持对 SQL 的转换。错误说明了一切。您不能在 LINQ to SQL 中使用正则表达式。

尝试使用likeorsubstring比较:

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

或者,您可以执行内存正则表达式比较。ToList()您可以通过在使用之前调用来做到这一点Regex

Regex R = new Regex("my regex");
var q = from c in db.tble1.ToList()
        where R.IsMatch(c.text)
        select c;
于 2012-06-07T17:21:38.823 回答