Find a list of Windows Services by given criteria My goal is to implement a method, which finds a list of Windows Services that match given set of criteria:


SearchString: A search string
SearchServiceBy: Could be ServiceName or DisplayName
SearchOption: Could be Equals, Contains, StartsWith, EndsWith or Regex
SearchServiceType: Could be SingleService or GroupOfServices




private List&lt;ServiceController&gt; GetServices()
{
    var allServices = ServiceController.GetServices().ToList();
    var resultList = new List&lt;ServiceController&gt;();

    switch (SearchOption)
    {
        case SearchOption.Equals:
            if (SearchServiceBy == SearchServiceBy.ServiceName)
            {
                resultList = allServices.Where(s =&gt; s.ServiceName.Equals(SearchString)).ToList();
            }
            else if (SearchServiceBy == SearchServiceBy.DisplayName)
            {
                resultList = allServices.Where(s =&gt; s.DisplayName.Equals(SearchString)).ToList();
            }
            break;

        case SearchOption.Contains:
            if (SearchServiceBy == SearchServiceBy.ServiceName)
            {
                resultList = allServices.Where(s =&gt; s.ServiceName.Contains(SearchString)).ToList();
            }
            else if (SearchServiceBy == SearchServiceBy.DisplayName)
            {
                resultList = allServices.Where(s =&gt; s.DisplayName.Contains(SearchString)).ToList();
            }
            break;

        case SearchOption.StartsWith:
            if (SearchServiceBy == SearchServiceBy.ServiceName)
            {
                resultList = allServices.Where(s =&gt; s.ServiceName.StartsWith(SearchString)).ToList();
            }
            else if (SearchServiceBy == SearchServiceBy.DisplayName)
            {
                resultList = allServices.Where(s =&gt; s.DisplayName.StartsWith(SearchString)).ToList();
            }
            break;

        case SearchOption.EndsWith:
            if (SearchServiceBy == SearchServiceBy.ServiceName)
            {
                resultList = allServices.Where(s =&gt; s.ServiceName.EndsWith(SearchString)).ToList();
            }
            else if (SearchServiceBy == SearchServiceBy.DisplayName)
            {
                resultList = allServices.Where(s =&gt; s.DisplayName.EndsWith(SearchString)).ToList();
            }
            break;

        case SearchOption.Regex:
            var pattern = new Regex(SearchString);

            if (SearchServiceBy == SearchServiceBy.ServiceName)
            {
                resultList = allServices.Where(s =&gt; pattern.IsMatch(s.ServiceName)).ToList();
            }
            else if (SearchServiceBy == SearchServiceBy.DisplayName)
            {
                resultList = allServices.Where(s =&gt; pattern.IsMatch(s.DisplayName)).ToList();
            }
            break;

        default:
            throw new ArgumentException("Unknown SearchOption: {0}", SearchOption.ToString());
    }

    if (SearchServiceType == SearchServiceType.SingleService
        &amp;&amp;
        resultList.Count &gt; 1)
    {
        resultList = new List&lt;ServiceController&gt; { resultList.FirstOrDefault() };
    }

    return resultList;
}


Is there any better way to implement this?

There is no additional complex logic around the method invoke. The use case is simple: Wix Toolset Custom Action passes required parameters to the method, gets a list of services match the given criteria. So these services could be started/stopped further.
These are my suggestions:


Get rid of all the ugly and duplicate ifs by replacing them with two dictionaries where each one returns a Func.
Use Regex for all searching criteria by creating appropriate patterns or returning the SearchString if it's the pattern.


Example:

var propertygGetters = new Dictionry&lt;SearchServiceBy, Func&lt;Service, string&gt;&gt;
{
    [SearchServiceBy.ServiceName] =&gt; s =&gt; s.ServiceName,
    ...
}

var patternCreators = new Dictionry&lt;SearchOption, Func&lt;string, string&gt;&gt;
{
    [SearchOption.Equals] =&gt; s =&gt; $"^{s}$",
    [SearchOption.Contains] =&gt; s =&gt; $"{s}",
    [SearchOption.StartsWith] =&gt; s =&gt; $"^{s}",
    [SearchOption.EndsWith] =&gt; s =&gt; $"{s}$",
    [SearchOption.Regex] =&gt; s =&gt; s,
}

var getProperty = searchStringGetters[SearchServiceBy];
var createPattern = patternCreators[SearchOption];

var pattern = createPattern(SearchString);

return 
    allServices
        .Where(service =&gt; Regex.IsMatch(getProperty(service), pattern))
        .ToList();


(warning: notepad programming)