A smart alternative in LINQ to iterate over a HashSet <string>
I have a whitelist of the URLs I use inside a HashSet<string> . I am trying to find if the url starts with any of the elements in the white list (it should be this way round).
Edit: the previous example was a bit misleading and had a typo - I already have a base url like yahoo.com, whitelisting is just the way to go.
HashSet<string> whiteList = new HashSet<string>(); string path = "/sport/baseball/"; bool validUrl = false; foreach (string item in whiteList) { if (path.StartsWith(item)) { validUrl = true; break; } } Is there a more elegant way to do this search using LINQ (for objects)? The list is small, so performance is not a problem.
bool validUrl = whiteList.Any(item => linkUrl.StartsWith(item)); By the way, hash tables are not good data structures for such problems (where you do not have a key and if the key is based on a function), since you have to list the whole table all the time. You can use a simple List<string> to hold items and you will get better performance.
The problem here is with the search. Do you have regularity in the white list? those. will it always be the domain you need, and not necessarily the pages inside or a specific subdomain?
If so, you can use string.split to grab the first part of the URL from your string, and then use the .Contains () method of your hashset to get the element. This will remove the string.StartsWith () command, which is run once for each item in the list and compares the expensive string, and replaces it with a single output from string.split and O (1) looking for your hash.
HashSet<string> whiteList = new HashSet<string>(); //add items string urlStartsWith = "http://www.yahoo.com"; bool validURL = whiteList.Contains(url);