Hi there,
I would like to know if it is possible to do a wildcard search using LINQ.
I see LINQ has Contains, StartsWith, EndsWith, etc.
What if I want something like %Test if%it work%, how do I do it?
Regards
Hi there,
I would like to know if it is possible to do a wildcard search using LINQ.
I see LINQ has Contains, StartsWith, EndsWith, etc.
What if I want something like %Test if%it work%, how do I do it?
Regards
You can use SqlMethods.Like().
An example of the usage:
var results =
from u in users
where SqlMethods.Like(u.FirstName, "%John%")
select u;
Are you talking LINQ to objects or LINQ to SQL?
For LINQ to objects you'll have to resort to regular expressions me thinks.
not sure if you talk LinqToSql or just linq... but you could regular expressions like this:
.Where(dto => System.Text.RegularExpressions.Regex.IsMatch(dto.CustomerName, @"Ad"));
add System.Data.Linq.SqlClient to your using or imports list then try:
var results= from x in data
where SqlMethods.Like(x.SearchField, “%something%like%this%”)
select x;
I would use Regular Expressions, since you might not always be using Linq to SQL.
Like this example of Linq to Objects
List<string> list = new List<string>();
list.Add("This is a sentence.");
list.Add("This is another one.");
list.Add("C# is fun.");
list.Add("Linq is also fun.");
System.Text.RegularExpressions.Regex regEx = new System.Text.RegularExpressions.Regex("This");
var qry = list
.Where<string>(item => regEx.IsMatch(item))
.ToList<string>();
// Print results
foreach (var item in qry)
{
Console.WriteLine(item);
}