-5

I have a model with a enum property. When I call my service, the model is returned back, and my enum-property contains the following data: Test1 | Test2 | Test3.

I want to loop through the propery and assign the values to a list.

How can I do this with an enum propery?

var form = await _formService.GetById();

That code above generates a result with a enum property called Sections with th data that I provided above, but I don't know how to loop through It to get the values.

Here is my Enum:

[Flags]
public enum Sections
{
    Test1= 0,
    Test2= 1,
    Test3= 2,
    Test4= 4,
}
Bryan
  • 3,421
  • 8
  • 37
  • 77
  • 3
    Talk is cheap, show the code. – tym32167 Nov 08 '16 at 15:36
  • @tym32167: Check the updated question. – Bryan Nov 08 '16 at 15:38
  • Showing the `enum` defintion would help enumerously. Does it use [Flags] attribute? Or is your property a list or an array? – Peter B Nov 08 '16 at 15:39
  • @PeterB: Check the updated Question. It uses the Flags. – Bryan Nov 08 '16 at 15:41
  • 1
    Possible duplicate of [How do I enumerate an enum?](http://stackoverflow.com/questions/105372/how-do-i-enumerate-an-enum) – Peter B Nov 08 '16 at 15:42
  • @PeterB: No. I don't wnat to enumerate the whole enum. I want to enumerate the enum-property with the result data. – Bryan Nov 08 '16 at 15:44
  • You'll still need to enumerate the whole enum and repeatedly check the value of `prop.HasFlag(enumValue)`. You can not enumerate `prop` itself. See https://msdn.microsoft.com/en-us/library/system.enum.hasflag(v=vs.110).aspx – Peter B Nov 08 '16 at 15:48

2 Answers2

0

Is this what you are looking for?

    [Flags]
    public enum Sections
    {
        Test1 = 0,
        Test2 = 1,
        Test3 = 2,
        Test4 = 4,
    }

    public static List<Sections> getSectionsFromFlags(Sections flags)
    {
        var returnVal = new List<Sections>();
        foreach (Sections item in Enum.GetValues(typeof(Sections)))
        {
            if ((int)(flags & item) > 0)
                returnVal.Add(item);
        }
        return returnVal;
    }
Theo
  • 885
  • 6
  • 16
0

If you have defined enum like this

[Flags]
public enum Sections
{
    Test1 = 0,
    Test2 = 1,
    Test3 = 2,
    Test4 = 4,
}

Then

var someValue = Sections.Test1 | Sections.Test3 | Sections.Test4;   
var values = Enum.GetValues(typeof(Sections))
                 .OfType<Sections>().Where(x=>(x&someValue)==x)
                 .ToArray();    

values now contains all three value Sections.Test1 | Sections.Test3 | Sections.Test4

Another solution (from comments)

      var values = Enum.GetValues(typeof(Sections))
                  .OfType<Sections>()
                  .Where(x=>someValue.HasFlag(x))
                  .ToArray();

Last one Is most correct, I think.

tym32167
  • 4,741
  • 2
  • 28
  • 32
  • FYI, you want to use an & in your lamda where test. This `x=>(x|someValue)==someValue` will always be true. Use this instead: `x=>(x & someValue)==someValue` – Theo Nov 08 '16 at 15:53
  • @Theo can you show example please? If I want to use & where? – tym32167 Nov 08 '16 at 15:54
  • @Theo probably in case of & it should be ```(x&someValue)==x)``` – tym32167 Nov 08 '16 at 15:59
  • @tym32167: That don't work. When I replace "someValue" with form.Sections(my property thjat contains the values), I get error: "Operator & cannot be applied to operands of type Sections – Bryan Nov 08 '16 at 16:08
  • @Bryan Use second solution with ```HasFlag``` – tym32167 Nov 08 '16 at 16:38
  • @Bryan so, you need to show the code, where this error occurs. – tym32167 Nov 09 '16 at 11:26