Search code examples
c#.netlistdictionarysearch-engine

Need help implementing a list of some kind for three values


Working on a new way of using an already existing searchengine in an application. The idea is to use prefixes to search, instead of checking preferred values in checkboxes before searching. Something like this: "#12345678" whereas the searchengine would return a result for a telephone-number.

The problem is that this search consists of three values. I have two enums SearchCriteria and SearchType. The third value is the actual prefix as a string value.

What is the most efficient way of solving this? Been looking in to KeyValuePairs, but this seems to end up a bit messy.

I've tried holding the different possibilities in a Dictionary aswell.

prefixes = new Dictionary<string, KeyValuePair<SearchCriteria, SearchType>> {
    { values },
    { values }
}

Suggestions please? :)


Solution

  • What's wrong with just writing a simple value type to hold this information?

    public struct Search
    {
        public Search(SearchCriteria criteria, SearchType type, string prefix) { ... }
    
        public readonly SearchCriteria Criteria;
        public readonly SearchType Type;
        public readonly String Prefix;
    }
    

    Putting your various search types into a list you can use linq to get the type & criteria for a specific prefix...

    Search[] searches = new []
    {
        new Search(SearchCriteria.PhoneNumber, SearchType.Match, "#"),
        new Search(SearchCriteria.PostCode, SearchType.CaseInsensetive, "*")
    }
    
    string prefix = "#";
    
    var searchCriteria = searches.Single(x => x.Prefix = prefix).Criteria;
    var searchType     = searches.Single(x => x.Prefix = prefix).Type;