How to remove an enumeration element from an array

In C #, how can I remove elements from an enum array?

Here is the listing:

public enum example { Example1, Example2, Example3, Example4 } 

Here is my code to get the listing:

 var data = Enum.GetValues(typeof(example)); 

How to remove Example2 from a data variable? I tried to use LINQ, however I'm not sure if this can be done.

+5
source share
2 answers

You cannot remove it from the array itself, but you can create a new array that does not have an Example2 element:

 var data = Enum .GetValues(typeof(example)) .Cast<example>() .Where(item => item != example.Example2) .ToArray(); 
+7
source

I tried to use LINQ, but I'm not sure if this can be done.

If you just want to exclude Example2

 var data = Enum .GetValues(typeof(example)) .Cast<example>() .Where(item => item != example.Example2); 

If you want to exclude two or more listings

 var data = Enum.GetValues(typeof(example)) .Cast<example>() .Except(new example[] { example.Example2, example.Example3 }); 
+5
source

All Articles