0
public enum MyEnum
{
    A,
    Ab,
    Abc,
    Abcd,
    Abcde
}

Using LINQ, I want to extract a list from MyEnum that contains all the items of MyEnum except the items Ab and Abc.

Stacked
  • 6,892
  • 7
  • 57
  • 73
  • 4
    The first google result gave me the answer already. Please show some effort next time. http://msdn.microsoft.com/en-us/library/system.enum.getvalues.aspx – Jeroen Vannevel Sep 19 '13 at 20:35
  • possible duplicate of [How do I convert an enum to a list in C#?](http://stackoverflow.com/questions/1167361/how-do-i-convert-an-enum-to-a-list-in-c) – Bob. Sep 19 '13 at 20:37
  • Do you want a list of strings with the names or a list of MyEnums set to all the values? – Hogan Sep 19 '13 at 20:39
  • 1
    @JeroenVannevel You did not understand my question body, I know about Enum.GetValues() but it's not what I'm asking. you should show more effort to analyze the questions instead of fast reacting – Stacked Sep 19 '13 at 23:59
  • @Bob. Thanks for your comment but this is not what I'm asking. – Stacked Sep 20 '13 at 00:09

3 Answers3

7
var doNotUse = new[] { MyEnum.Ab, MyEnum.Abc };
var enums = Enum.GetValues(typeof(MyEnum)).Cast<MyEnum>()
    .Where(me => !doNotUse.Contains(me))
    .ToList();
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
1
var list = Enum.GetNames(typeof(MyEnum))
               .Where(r=> r != "Abc" && r != "Ab")
               .ToList();

For output:

foreach(var item in list)
    Console.WriteLine(item);

Output:

A
Abcd
Abcde
Habib
  • 219,104
  • 29
  • 407
  • 436
0

you don't need linq :

    public void test()
    {
        List<MyEnum> list = new List<MyEnum>();

        foreach (MyEnum item in Enum.GetValues(typeof(MyEnum)))
        {
            list.Add(item);
        }
    }
CheapD AKA Ju
  • 713
  • 2
  • 7
  • 21