Anyway to make a IList.Contains() act more like a wildcard contains?
Question
I am trying to parse thru a csv string, put the results into a IList collection and then trying to find a way to do a wildcard 'contains' based on what was passed in. Right now I have the following:
public static IList<string> DBExclusionList
{
get
{
Regex splitRx = new Regex(@",\s*", RegexOptions.Compiled);
String list = (string)_asr.GetValue("DBExclusionList",typeof(string));
string[] fields = splitRx.Split(list);
return fields;
}
}
if (DBExclusionList.Contains(dbx.Name.ToString())==false)
{...}
So if the string I am parsing (key value from .config file) contains: key="DBExclusionList" value="ReportServer,ReportServerTempDB,SQLSentry20,_TEST"
The DBExclusionList.Contains() works very well for exact matches on the first 3 items in the list, but I want to be able to ALSO have it for any partial match of the fourth item '_TEST'
is there any way to do it? I could certainly hardcode it to always exclude whatever but I'd rather not.
thanks.
Solution
Using Linq :
if (DBExclusionList.Any(s => s.Contains(dbx.Name.ToString())))
OTHER TIPS
Since you're using .NET 3.5, you could use the Where() LINQ extension method:
DBExclusionList.Where(item => item.Contains(dbx.Name.ToString()))
Don't use regex to split. Use string.Split()
and delimit your _asr.GetValue("DBExclusionList)
with something like semi-colons.
Each item in _asr.GetValue("DBExclusionList)
should be a regex pattern, so you check against each one in turn.