Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cast Int Array to Enum Flags

Tags:

c#

I have the following enum with flags:

[Flags]
public enum DataFiat {
  Public = 1,
  Listed = 2,
  Client = 4
} // DataFiat

And I have an int array, for example:

int[] selected = new int[] { 1, 4 }

How can I convert this to my enum which would become:

DataFiat.Public | DataFiat.Client

Thank You, Miguel

like image 602
Miguel Moura Avatar asked Feb 19 '14 12:02

Miguel Moura


3 Answers

var f = (DataFiat)selected.Sum();
like image 92
stepandohnal Avatar answered Oct 16 '22 18:10

stepandohnal


How about something like

var tt = (DataFiat)selected.Aggregate((i, t) => i | t);
like image 4
Adriaan Stander Avatar answered Oct 16 '22 18:10

Adriaan Stander


this snippet:

        var intArr = new[] { 1, 4 };
        var sum = intArr.Sum(x => x);
        var result = (Test)sum;

returns

enter image description here

like image 3
csteinmueller Avatar answered Oct 16 '22 20:10

csteinmueller