title: Create a List of objects using enum data C#
body: <p>So I have an enum:</p>
  
  <pre><code>public enum myEnum
  {
      IBM = 1,
      HP = 2,
      Lenovo = 3
  }
  </code></pre>
  
  <p>I have a <code>Brand</code> class</p>
  
  <pre><code>public class Brand
  {
      public Brand(string name, int id)
      {
          Name = name;
          Id = id;
      }
  
      public string Name { get; private set; }
      public int Id { get; private set; }
  }
  </code></pre>
  
  <p>I want to create a List of <code>Brand</code> objects populated with the data from the <code>MyEnum</code>. Something like:</p>
  
  <pre><code>private IEnumerable&lt;Brand&gt; brands = new List&lt;Brand&gt;
  {
      new Brand(myEnum.IBM.ToString(), (int) myEnum.IBM),
      new Brand(myEnum.HP.ToString(), (int) myEnum.HP),
      new Brand(myEnum.Lenovo.ToString(), (int) myEnum.Lenovo),
  };
  </code></pre>
  
  <p>I can create two arrays - one with enum names and the other with enum ids and foreach them to create Brand object for every iteration but wonder if there is a better solution.</p>
  
  <p>Finally, I am going with Royi Mindel solution as according to me it is the most appropriate . Many thanks to Daniel Hilgarth for his answer and his help making Royi Mindel suggestion work. I would give credit to both of them for this question if I could.</p>
  
  <pre><code>public static class EnumHelper
  {
      public static IEnumerable&lt;ValueName&gt; GetItems&lt;TEnum&gt;() 
          where TEnum : struct, IConvertible, IComparable, IFormattable
      {
          if (!typeof(TEnum).IsEnum)
              throw new ArgumentException("TEnum must be an Enumeration type");
  
          var res = from e in Enum.GetValues(typeof(TEnum)).Cast&lt;TEnum&gt;()
                    select new ValueName() { Id = Convert.ToInt32(e), Name = e.ToString() };
  
          return res;
      }
  }
  
  public struct ValueName
  {
      public int Id { get; set; }
      public string Name { get; set; }
  }
  </code></pre>