1

i have the following code

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

and while debugging i saw in the q result this message

Method 'Boolean IsMatch(System.String)' has no supported translation to SQL."} System.SystemException {System.NotSupportedException}

so what have i done wrong ?

Edit:I learned that the method has no supported translation to SQL
but how to solve this

1 Answer 1

9

Regex has no supported translation to SQL. The error says it all. You can't use regex in LINQ to SQL.

Try using a like or substring comparison instead:

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

Or, you could perform an in memory regex comparison. You can do this by calling ToList() before using the Regex:

Regex R = new Regex("my regex");
var q = from c in db.tble1.ToList()
        where R.IsMatch(c.text)
        select c;
Sign up to request clarification or add additional context in comments.

1 Comment

@Star yes, but again: TSQL does not support regex. The only option left is a SQL-CLR function that you write and install on the server, and it won't be efficient (it will table scan)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.