Dukhabandhu Sahoo
Dukhabandhu Sahoo

Reputation: 1424

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)

Upvotes: 4

Views: 1114

Answers (1)

Dukhabandhu Sahoo
Dukhabandhu Sahoo

Reputation: 1424

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();

Upvotes: 9

Related Questions