Simple Question how to have something like this
customers.where(c=>c.Name **like** "john");
i know this isn not possible but i was wondering how can i have something similar.
thanks in advance.
Simple Question how to have something like this
customers.where(c=>c.Name **like** "john");
i know this isn not possible but i was wondering how can i have something similar.
thanks in advance.
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:
var matches = people.Where(p => Regex.IsMatch(p.Name, "A.*[mn]"));
foreach (Person match in matches)
{
Console.WriteLine(match.Name);
}
And get output like this:
Adam Aaron Aidan
Going with string.Contains
as others have suggested is almost certainly preferable if your intention is simply to look for a specific substring within Name
.
Use SqlMethods.Like:
customers.Where(c => SqlMethods.Like(c.Name, "%john%"));
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, the SqlMethods
class and its members are used as a "hint" for the expression compiler (compiles expression trees to SQL) to emit a LIKE
statement.
Use Regex.IsMatch
in your where statement or for a more simpler version without wildcards etc.:
customers.where(c=>c.Name.Contains("john"));
using System.Data.Linq.SqlClient;
...
customers.where(c=>SqlMethods.Like(c.Name, "john"));