0

I am currently facing a challenge passing a regex query to the FromSqlRaw method in EF Core >=3.1.

The FromSqlRaw treats the query as string interpolation. I want it to ignore this and accept the query as it is.

Sample regex to test with

^.*([a-z]\\s*){3,4}([0-9]\\s*){3}([a-z]?){1}[^$].*$

Example 2

this.context.Mudal.FromSqlRaw(@"SELECT * FROM Mudal WHERE replace(BillRefNumber, ' ', '') regexp '^.*([a-z]\\s*){3,4}([0-9]\\s*){3}([a-z]?){1}[^$].*$';").ToList()

2 Answers 2

1

You could try using parameterization to pass the regular expression. Try the following:

string regexpression = @"^.*([a-z]\\s*){3,4}([0-9]\\s*){3}([a-z]?){1}[^$].*$";
var mylist = this.context.Mudal
   .FromSqlRaw(@"SELECT * FROM Mudal WHERE replace(BillRefNumber, ' ', '') regexp {0};", 
      regexpression)
   .ToList();
Sign up to request clarification or add additional context in comments.

3 Comments

Could you add an example to your original post showing what you are currently trying?
@JohnNyingi Modified the answer after update to question.
have you tested this?
0

I found an ideal answer all I have to do is change from this {1} to this {{1}}. This acts as an escape from string interpolation.

Comments

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.