Skip to content

Instantly share code, notes, and snippets.

@slpsys
Created May 9, 2011 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slpsys/963093 to your computer and use it in GitHub Desktop.
Save slpsys/963093 to your computer and use it in GitHub Desktop.
Filtering enum values by a set of strings we want to display, with full type information.
// Some enum, could be RateFamilyTypes
enum Letters { A, B, C}
void Main()
{
// List of stringified subset of enum values; could be sourced from XAML
HashSet<string> filterList = new HashSet<string>() { "B" };
// Get as typesafe array of enum values (allLetters is an array of Letters)
var letterArray = Enum.GetValues(typeof(Letters));
Letters[] allLetters = new Letters[letterArray.Length];
letterArray.CopyTo(allLetters, 0);
// Get an IEnumerable or Array of typed enum values, based on the filter list
var filteredList = allLetters.Where(x => filterList.Contains(x.ToString())).ToArray();
// Display in dropdown with full type information (filteredList is also an array of Letters)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment