Reputation: 1424
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
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