Link to home
Start Free TrialLog in
Avatar of dingir
dingirFlag for Sweden

asked on

Query enum as a list result

How do I perform a list of an enum?

Like

var result =  from u in nameSpace.Enum
                     select u;

??
ASKER CERTIFIED SOLUTION
Avatar of gemailj
gemailj
Flag of Egypt image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi dingir,

I am not entirely clear what you mean, but hopefully one of these will be what you are after - if not please clarify your question:

// Example assumes the enum Gender:
 // Male = 0,
 // Female = 1
 
 int result  = (int)Gender.Female; // result == 1

 string result = Gender.Female.ToString(); // result == 'Female'

Chris Bray

// Assumes the enum Gender:
// Male,
// Female

int result  = (int)Gender.Female; // result == 1
string result = Gender.Female.ToString(); // result == 'Female'

Open in new window

Avatar of dingir

ASKER

Thank's chris, sorry for being unclear. However I think gemailj ranned exactly into my point. I just need to think how to fit this into a dropdown together with a id value and a name value.
        opAttributeTypes.DataSource = Core.GetAttribute.Titles;
        opAttributeTypes.DataBind();

----------------

        public static List<AttributeType> Titles
        {
            get
            {
                //DataContext db = new DataContext();
                //List<Titles> attribute = 
                //    (from a in db.Attributes
                //     group a by new { a.AttributeType } into gr
                //      select new Titles
                //      {
                //          AttributeId = (int)gr.Key.AttributeType,
                //          Name = (AttributeType)gr.Key.AttributeType,
                //      }).ToList();

                List<AttributeType> attribute = new List<AttributeType>();
                AttributeType[] values = (AttributeType[])Enum.GetValues(typeof(AttributeType));
                foreach (AttributeType v in values)
                    attribute.Add(v);

                return attribute;
            }
        }

Open in new window

Avatar of dingir

ASKER

however, it's an superclear answer.. :)
Hi dingir:

It all depends on what you are trying to achieve - again pretty much insufficient information.

However, based on my earlier example the attached code may help

            // Gender
            genderTable = new DataTable();
            DataColumn primaryColumn = new DataColumn("Id", typeof(int));
            genderTable.Columns.Add(primaryColumn);
            genderTable.Columns.Add(new DataColumn("Gender", typeof(string)));
            genderTable.Columns.Add(new DataColumn("Value", typeof(int)));

            genderTable.PrimaryKey = new DataColumn[] { primaryColumn };
           
            int val;
            int count = 0;
            foreach (string str in Enum.GetNames(typeof(Gender)))
            {
                val = (int)Enum.Parse(typeof(Gender), str); // this is the int value of the enum
                // str is the name in the enum
                // this you can add to a datable            

                DataRow row = genderTable.NewRow();
                row["Id"] = count++;
                row["Gender"] = str;
                row["Value"] = val;
                genderTable.Rows.Add(row);            
            }

            genderAutoCompleteCombo.DataSource = genderTable;
            genderAutoCompleteCombo.DisplayMember = "Gender";
            genderAutoCompleteCombo.ValueMember = "Value";
            genderAutoCompleteCombo.DataBindings.Add("SelectedValue", adapter.Table, "GenderId");



// Gender
            genderTable = new DataTable();
            DataColumn primaryColumn = new DataColumn("Id", typeof(int));
            genderTable.Columns.Add(primaryColumn);
            genderTable.Columns.Add(new DataColumn("Gender", typeof(string)));
            genderTable.Columns.Add(new DataColumn("Value", typeof(int)));

            genderTable.PrimaryKey = new DataColumn[] { primaryColumn };
            
            int val;
            int count = 0;
            foreach (string str in Enum.GetNames(typeof(Gender)))
            {
                val = (int)Enum.Parse(typeof(Gender), str); // this is the int value of the enum
                // str is the name in the enum
                // this you can add to a datable            

                DataRow row = genderTable.NewRow();
                row["Id"] = count++;
                row["Gender"] = str;
                row["Value"] = val;
                genderTable.Rows.Add(row);            
            }

            genderAutoCompleteCombo.DataSource = genderTable;
            genderAutoCompleteCombo.DisplayMember = "Gender";
            genderAutoCompleteCombo.ValueMember = "Value";
            genderAutoCompleteCombo.DataBindings.Add("SelectedValue", adapter.Table, "GenderId");

Open in new window

Of course, if you don't need it databound you can do it this way:

Chris Bray

[C#]
// Setup the binding as follows:
// MyValues is the enum type
comboBox1.DataSource = Enum.GetValues(typeof MyValues);


// Then in the SelectedValueChanged event for the ComboBox.

[C#]
private void ComboBox1ValueChanged(object sender, EventArgs e)
{
MyValues v = (MyValues)this.comboBox1.SelectedValue;
}

Open in new window