title: How can I return a Dictionary<int, object>?
body: <p>This works:</p>
  
  <pre><code>public IDictionary&lt;int, object&gt; GetProducts( int departmentID )
  {
      return new Dictionary&lt;int, object&gt;
                  {
                      { 1, new { Description = "Something" } },
                      { 2, new { Description = "Whatever" } },
                  };
  }
  </code></pre>
  
  <p>But for some reason this doesn't:</p>
  
  <pre><code>public IDictionary&lt;int, object&gt; GetProducts( int departmentID )
  {
      var products = ProductRepository.FindAll( p =&gt; p.Department.Id == departmentID );
  
      return products.ToDictionary( p =&gt; p.Id, p =&gt; new { Description = p.Description } );
  }
  </code></pre>
  
  <p>This doesn't work either:</p>
  
  <pre><code>public IDictionary&lt;int, object&gt; GetProducts( int departmentID )
  {
      var products = ProductRepository.FindAll( p =&gt; p.Department.Id == departmentID );
  
      return products.ToDictionary( p =&gt; p.Id, p =&gt; new { p.Description } );
  }
  </code></pre>
  
  <p>The compiler error (in both cases) is:</p>
  
  <pre><code>Cannot convert expression type 'System.Collections.Generic.Dictionary&lt;int,{Description:string}&gt;' to return type 'System.Collections.Generic.IDictionary&lt;int,object&gt;'
  </code></pre>
  
  <p>I assumed it was a problem with the ToDictionary Linq extension method, but according to <a href="https://stackoverflow.com/a/1619530/1015595">this answer</a> it should work since FindAll returns an <code>IQueryable&lt;Product&gt;</code>:</p>
  
  <blockquote>
    <p>... if your data is coming from an IEnumerable or IQueryable source, you can
    get one using the LINQ ToDictionary operator and projecting out the
    required key and (anonymously typed) value from the sequence elements:</p>
  
  <pre><code>var intToAnon = sourceSequence.ToDictionary(
      e =&gt; e.Id,
      e =&gt; new { e.Column, e.Localized });
  </code></pre>
  </blockquote>
  
  <p>What gives?</p>