title: Serialize Enum Values and Names to JSON
body: <p>This question is related, but IMHO not identical to</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/331976/how-do-i-serialize-a-c-sharp-anonymous-type-to-a-json-string/331983#331983">How do I serialize a C# anonymous type to a JSON string?</a></li>
  <li><a href="https://stackoverflow.com/questions/14776177/serialize-c-sharp-enum-definition-to-json">Serialize C# Enum Definition to Json</a></li>
  </ul>
  
  <p>Whilst testing, I've also stumbled across this culprit LinqPad which made my life difficult: 
  <a href="https://stackoverflow.com/questions/23276970/why-does-linqpad-dump-enum-integer-values-as-strings">Why does LINQPad dump enum integer values as strings?</a></p>
  
  <p>Now, my actual question: 
  My application (in particular SyncFusion component datasources, such as MultiSelect) requires enumerations in JSON format, e.g. something like this:</p>
  
  <pre><code>[ {"Id":0,"Name":"Unknown"},{"Id":1,"Name":"Open"},{"Id":2,"Name":"Closed"},{"Id":3,"Name":"Approve"} ]
  </code></pre>
  
  <p><strong>UPDATE</strong>
  As dbc pointed out, my question may not have been clear enough. I do not want to serialize one entry of the enumeration, but the whole struct. The JSON could then be used for a data source in Javascript, e.g. for a , simplified:
  <code>
  &lt;option value=0&gt;Unknown&lt;/option&gt;
  &lt;option value=1&gt;Open&lt;/option&gt; etc</code></p>
  
  <p>The JSON object is identical to an Enum in a namespace (with the exception that I have given the a property name to the Key and Value of each entry:</p>
  
  <pre><code>public enum ListOptions
  {
      Unknown = 0,
      Open = 1,
      Closed = 2,
      Approve = 3
  }
  </code></pre>
  
  <p>I've struggled with Enums, all the other approaches such as specifying a Json StringConverter etc did't yield <em>all</em> options in an array, so I ended up using Linq. My View Model now has a string property like this:</p>
  
  <pre><code>public string CrewListOption =&gt; JsonConvert.SerializeObject(Enum.GetValues(typeof(ListOptions))
          .Cast&lt;int&gt;()
          .Select(e =&gt; new { Id = (int)  e, Name = typeof(ListOptions).GetEnumName(e) }));
  </code></pre>
  
  <p>Given that I'm pretty much a beginner with ASP.Net Core, I find it hard to believe that this should be a good solution. Yet I find it hard to find straight-forward better examples of the same thing.</p>
  
  <p>I'd appreciate it if you might be able to help me improve this, and make it potentially more generically useful to "export" whole enumerations to JSON. </p>
  
  <p>Here's the full LinqPad (where Newtonsoft.Json is imported from GAC):</p>
  
  <pre><code>void Main()
  {   
      Enum.GetValues(typeof(ListOptions)).Cast&lt;int&gt;().Select(e =&gt; new { Id = e, Name = (ListOptions) e } ).Dump(); // these are identical, except for the typeof()
      Enum.GetValues(typeof(ListOptions)).Cast&lt;int&gt;().Select(e =&gt; new { Id = (int)  e, Name = typeof(ListOptions).GetEnumName(e) }).Dump(); // is typeof(MyEnumType) better?
      string JsonString = JsonConvert.SerializeObject(Enum.GetValues(typeof(ListOptions)).Cast&lt;int&gt;().Select(e =&gt; new { Id = (int)  e, Name = typeof(ListOptions).GetEnumName(e) }));
      JsonString.Dump(); // [{"Id":0,"Name":"Unknown"},{"Id":1,"Name":"Open"},{"Id":2,"Name":"Closed"},{"Id":3,"Name":"Approve"}]
  }
  
  public enum ListOptions { 
      Unknown = 0,
      Open = 1,
      Closed = 2,
      Approve = 3 
  };
  </code></pre>