简体   繁体   中英

Enum values to string list with filter

I need to return only the values from 1 omitting the Ant

    public enum AnimalCodeType
{
    Ant = 0,
    Koala = 1,
    Panda = 2,
} 

The below code gets me all values.. How do I change it

    return Enum.GetValues(typeof(AnimalCodeType)).Cast<AnimalCodeType>().Select(v => v.ToString()).ToList();        

This gives you all but the ant (recommended way):

var animalsWithoutAnt = Enum.GetValues(typeof(AnimalCodeType)).Cast<AnimalCodeType>()
    .Where(act => act != AnimalCodeType.Ant)
    .ToList();

or by using the int-value:

var animalsWithoutAnt = Enum.GetValues(typeof(AnimalCodeType)).Cast<AnimalCodeType>()
   .Where(act => (int)act != 0)
   .ToList();
return Enum
    .GetValues( typeof(AnimalCodeType) )
    .Cast<AnimalCodeType>()
    .Where( v => (int)v > 0 )
    .Select( v => v.ToString() )
    .ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM