[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 374
  • Last Modified:

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;

??
0
dingir
Asked:
dingir
  • 3
  • 2
1 Solution
 
gemailjCommented:
here you are a simple example how to do so
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            exampleEnum[] values = (exampleEnum[])Enum.GetValues(typeof(exampleEnum));
            foreach (exampleEnum v in values)
                MessageBox.Show(v.ToString("G") + " " + v.ToString("D"));
        }
    }

    enum exampleEnum
    {
        first,
        second,
        third,
        forth
    }

Open in new window

0
 
chrisbrayCommented:
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

0
 
dingirAuthor Commented:
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

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
dingirAuthor Commented:
however, it's an superclear answer.. :)
0
 
chrisbrayCommented:
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

0
 
chrisbrayCommented:
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

0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now