dingir
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;
??
Like
var result = from u in nameSpace.Enum
select u;
??
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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;
}
}
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(pr imaryColum n);
genderTable.Columns.Add(ne w DataColumn("Gender", typeof(string)));
genderTable.Columns.Add(ne w DataColumn("Value", typeof(int)));
genderTable.PrimaryKey = new DataColumn[] { primaryColumn };
int val;
int count = 0;
foreach (string str in Enum.GetNames(typeof(Gende r)))
{
val = (int)Enum.Parse(typeof(Gen der), 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.Da taSource = genderTable;
genderAutoCompleteCombo.Di splayMembe r = "Gender";
genderAutoCompleteCombo.Va lueMember = "Value";
genderAutoCompleteCombo.Da taBindings .Add("Sele ctedValue" , adapter.Table, "GenderId");
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(pr
genderTable.Columns.Add(ne
genderTable.Columns.Add(ne
genderTable.PrimaryKey = new DataColumn[] { primaryColumn };
int val;
int count = 0;
foreach (string str in Enum.GetNames(typeof(Gende
{
val = (int)Enum.Parse(typeof(Gen
// 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.Da
genderAutoCompleteCombo.Di
genderAutoCompleteCombo.Va
genderAutoCompleteCombo.Da
// 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");
Of course, if you don't need it databound you can do it this way:
Chris Bray
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;
}
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
Open in new window