Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the SQL LIKE operator equivalent in LINQ having multiple % operators in the input text?

To convert the SQL LIKE query to LINQ which having "%"(percentage) operators both at the start and end of the string, we use the Contains() method.

e.g.

SELECT * FROM [User] WHERE Username LIKE '%test%'

The equivalent LINQ is:

var users = (from usr in Context.Users
            where usr.Username.Contains("test")
            select usr).ToList();

What will be equivalent of the below query which contains multiple "%"(percentage) operators in the input text?

SELECT * FROM [User] WHERE Username LIKE '%test%email%'

Any help is appreciated.

Note: The query will be executed in the EntityFramework(version 6.1.3)

like image 517
Dukhabandhu Sahoo Avatar asked Oct 19 '25 05:10

Dukhabandhu Sahoo


1 Answers

Posting the comments given by @Fabio and @CodeNotFound as answer for reference.

In EntityFramework version 6.2.0:

var users = (from usr in Context.Users
            where DbFunctions.Like(usr.Username, "%test%email%")
            select usr).ToList();
like image 77
Dukhabandhu Sahoo Avatar answered Oct 21 '25 19:10

Dukhabandhu Sahoo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!