How can I do something like this:
customers.where(c=>c.Name **like** "john");
I know this isn't possible but I was wondering how can I have something similar.
How can I do something like this:
customers.where(c=>c.Name **like** "john");
I know this isn't possible but I was wondering how can I have something similar.
Here is my code :
Somethings like that.
The first thought that comes to mind is
Regex.IsMatch
.This would come closest to providing the kind of functionality you get from
LIKE
; for instance with it you could do this:And get output like this:
Going with
string.Contains
as others have suggested is almost certainly preferable if your intention is simply to look for a specific substring withinName
.If you are targeting LINQ to SQL, use SqlMethods.Like:
Explanation:
The compiler will generate an expression tree from the statement above. Since
LIKE
is a SQL specific construct and not common to all LINQ Query providers, theSqlMethods
class and its members are used as a "hint" for the expression compiler (compiles expression trees to SQL) to emit aLIKE
statement.Use
Regex.IsMatch
in your where statement or for a more simpler version without wildcards etc.: