title: Converting an generic IDictionary to an ASP.NET MVC IEnumerable<SelectListItem>: choosing the Selected item
body: <p>Here is the full implementation I am considering:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Web.Mvc;
  
  namespace Utils {
      public static class IDictionaryExt {
          public static IEnumerable&lt;SelectListItem&gt; ToSelectListItems&lt;T, R&gt;(this IDictionary&lt;T, R&gt; dic, T selectedKey) {
              return dic.Select(x =&gt; new SelectListItem() { Text = x.Value.ToString(), Value = x.Key.ToString(), Selected=(dynamic)x.Key == (dynamic)selectedKey });
          }
      }
  }
  </code></pre>
  
  <p>Notice the equality check using the dynamic casts: <code>(dynamic)x.Key == (dynamic)selectedKey</code>. Is this the best way to check equality between <code>selectedKey</code> and <code>x.Key</code> here? Based on @Gabe's comment in <a href="https://stackoverflow.com/questions/5808057/operator-cant-be-applied-to-type-t">Operator &#39;==&#39; can&#39;t be applied to type T?</a>, I believe it is: overload resolution is deferred to to runtime, but we do get "normal" overload resolution (i.e. considering <code>ValueType</code>s and other <code>Object</code>s with <code>==</code> overloads versus <code>Object</code>s with default reference equality).</p>