Reputation: 14108
When searching for a word in the search field I shall be enable to find a word for instance "cookies" by typing "Coo" or "oOk". what LinQ syntax do I need to create this functionality?
Beside, the functionality should take account to case insensitivity for instance a word named "ComPuTER" should be found by typing the word "MpUT"?
Upvotes: 0
Views: 115
Reputation: 27486
You can use contains
:
var query = from t in tbl
where t.ToUpper().Contains(input.ToUpper())
select t;
I used ToUpper to ensure that the table and the source are the same case, so that "Coo" will match "cookies"
There's also a Like method in SQLMethods, but I have not used this...
var query = from t in tbl
where SqlMethods.Like(t, "%"+input+"%")
select t;
(See more here: http://blogs.microsoft.co.il/blogs/bursteg/archive/2007/10/16/linq-to-sql-like-operator.aspx)
Upvotes: 1
Reputation: 1382
Here's one way using your values. Try it out in LinqPad. Doesn't really use any of the power of Linq though and you'll want to watch it for very large sources.
string[] words1 = {"cookies", "lasagna", "steak"};
string searchTerm1 = "Coo";
var matches1 = words1.Where (w => w.ToLowerInvariant().Contains(searchTerm1.ToLowerInvariant()));
Console.WriteLine(matches1.Count().ToString());
string searchTerm2 = "oOk";
var matches2 = words1.Where (w => w.ToLowerInvariant().Contains(searchTerm2.ToLowerInvariant()));
Console.WriteLine(matches2.Count().ToString());
List<string> words2 = new List<string>();
words2.AddRange(new string[] {"abacus", "ComPuTER", "coffee maker"});
string searchTerm3 = "MpUT";
var matches3 = words2.Where (w => w.ToLowerInvariant().Contains(searchTerm3.ToLowerInvariant()));
Console.WriteLine(matches3.Count().ToString());
Upvotes: 0
Reputation: 83699
something like:
var cookies = (from t in tbl where t.Contains("oOK") select t);
Upvotes: 0