Well, the easiest approach is to just get the name using ToString :
return ((Animals[]) Enum.GetValues(typeof(Animals))) .Where(r => r.ToString().StartsWith("Cat")) .Select(r => (int) r)
Or using Cast<> to avoid casting to Animals[] :
return Enum.GetValues(typeof(Animals)) .Cast<Animals>() .Where(r => r.ToString().StartsWith("Cat")) .Select(r => (int) r)
(I prefer from Cast<> to OfType<> here, since we really expect that each value will be Animals - if something goes wrong, the exception is completely fine!)
However, all this seems a little unpleasant to me - adding any value to the names of the enumerated values, you always feel a little hairy, and even more parsing specific bits of the name. You can try to decorate each value with an attribute to indicate a “group”.
I also suggest calling enum Animal , not Animals - only flag-based enumerations should usually be multiple, and the "collection of Animals values" sounds distinctly odd.
(In addition, you can look at my Unconstrained Melody project, which allows you to use safe and efficient shared access to type enumerations using common restrictions that are legal in IL but cannot be expressed in C # ...)
source share