I have a method similar to the following:
StringComparison comparison = StringComparison.CurrentCultureIgnoreCase;
switch(SearchType)
{
case SearchType.Contains:
list = list.Where(a => a.Reference.Contains("test",comparison));
break;
case SearchType.StartsWith:
list = list.Where(a => a.Reference.StartsWith("test",comparison));
break;
case SearchType.EndsWith:
list = list.Where(a => a.Reference.EndsWith("test",comparison));
break;
}
As you can probably guess SearchType is a custom enum I have.
Is there an easier way of doing this, possibily using reflection? The multiple switches seem a bit ugly.
Which part of that is not easy?
I guess you could have a class for each enum instead, inherit from a common SearchType interface and implement a function called ProcessList – not sure what list is to offer a better function.
Something like..
Need to do a class for each enum type.
Then you would need to set the SearchType variable like this…
and your switch could then be replaced with this…
…Not really easier in terms of coding, but you get a more readable code instead of the switch.