Reputation: 31
I am using Linq to Object, now how can i use some thing like "LIKE" used in sql query to filter data?
Upvotes: 3
Views: 4867
Reputation:
Try this
string searchString = "NAME";
List<string> lstNames = new List<string>()
{"Name1"
,"Name2"
,"Name3"
,"Other names"
,"Something else"};
The below query(s) will accomplish the work
var query1 = (from name in lstNames
where name.ToUpper().Contains(searchString)
select name);
var query2 = lstNames.FindAll(name => name.ToUpper().Contains(searchString));
var query3 = lstNames.Where(name => name.ToUpper().Contains(searchString));
Hope this helps
Upvotes: 2
Reputation: 62137
Seconded. Contains, StartsWith and EndsWith are the equivalents. There is no "LIKE 'a%b'", but this IS a rare case.
Upvotes: 0
Reputation: 2265
Use a Regex and call the IsMatch method. The % wildcard maps to .* and the ___ wildcard maps to ..
Upvotes: 1
Reputation: 39862
Use a combination of Contains
, StartsWith
, and EndsWith
. These are all methods or extension methods on System.String
and will work with LINQ. If this doesn't solve your problem, you will be forced to use regular expressions.
Upvotes: 3