Just to avoid confusion I am talking about linq to object and nothing else.
I am working with lots objects in memory and I need to filter them.
we have a screen with lots of options where a user can select many values.
I need to implement something similar to the “Like” operator in sql.
I did found a post about it in this site and uses regex but I have never used it.
I want to know if my string pattern is correct.Should not use “%” “%”?
I have also read that you should use a combination of startWith -endWith and contains,but I have not found any examples that uses all the them combined,just to get a feel how to do it.
I do something like this
string pattern = string.Format(".*{0}.*", criteria.SearchText);
myList= myList.Where(x => x.Message.Like(pattern)).ToList();
public static bool Like(this string s, string pattern, RegexOptions options = RegexOptions.IgnoreCase)
{
return Regex.IsMatch(s, pattern, options);
}
Any suggestions
If you want to check if a string contains another you can use:
or
(the second variant is good because with
IndexOfyou can specify theCultureInfo)If you want to mix a user-defined word in a
Regexyou should use theRegex.Escape()to escape it, so that if the user writesa*, the searched text isa*instead ofany number of a.But note that as written, it’s equivalent to:
because you haven’t put anchors to your regex, so the regex will be searched anywhere in the string.
If you want to anchor the user-defined word, so for example you want to search for words that begin with
or end with