There are times when I need to filter lists using a white list / black list style approach. For that purpose, I wrote two extensions methods that I’ll cover in this blog post.
The first method is called ApplyWhiteList
, and the listing for that looks like this:
public static IEnumerable<T> ApplyWhiteList<T>( this IEnumerable<T> sequence, Func<T, string> selector, string whiteList ) { Guard.Instance().ThrowIfNull(sequence, nameof(sequence)) .ThrowIfNull(selector, nameof(selector)); var whiteParts = whiteList.Split(','); return sequence.Where( x => whiteParts.Any(y => selector(x).IsMatch(y.Trim())) ).ToList(); }
This method starts by validating all the incoming parameters. Afterwards, it splits the list up, using a comma character as a separator. Once we have the two lists to work with, white list, and the sequence to be filtered, we combine them using the LINQ query. Notice the use of the IsMatch
extension method, in that query. IsMatch
is a method I’ve blogged about, before. It accepts a search string with wildcards in it. So, using IsMatch
, the white list can contain either strings, or strings with wildcards. Either will match, using the IsMatch
method.
The end result of this method is a list that contains all the elements of the original sequence that are also members of the white list. This is highly useful for ensuring that a list contains a minimal number of items, no matter what.
The next method I wanted to cover is called ApplyBlackList, and the listing for that method looks like this:
public static IEnumerable<T> ApplyBlackList<T>( this IEnumerable<T> sequence, Func<T, string> selector, string blackList ) { Guard.Instance().ThrowIfNull(sequence, nameof(sequence)) .ThrowIfNull(selector, nameof(selector)); var blackParts = blackList.Split(','); return sequence.Where( x => !blackParts.Any(y => selector(x).IsMatch(y.Trim())) ).ToList(); }
This method starts by validating the incoming parameters. Afterwards, it splits the black list using a comma character as a separator. Once I have the two lists, black list and sequence to be filtered, I then combine them using the LINQ query. Again, I use the IsMatch
method to allow for the use of embedded wildcard characters in the black list.
The end result of this method call is that the sequence no longer contains any elements that match the given black list.
Using these methods is pretty simple. Here’s an example that combines the white and black lists:
var list = new string[] {"A", "B", "C", "D", "E", "F", "G"}; var black = "A, D, E, F"; var white = "B, D, H"; var result = list.ApplyWhiteList(white).ApplyBlackList(black); // result contains: B, C, G, H
These methods are part of the CG.Core NUGET package. The source can be found HERE. The package itself can be found HERE.
Thanks for reading!
Photo by Martin Sanchez on Unsplash