title: EF returning different values than query
body: <p>So I just came across this very odd scenario and was wondering if anyone might know what the problem is.  I have the following EF Linq query.</p>
  
  <pre><code>var hierarchies = (from hierarchy in ctx.PolygonHierarchyViews
                     where hierarchy.DashboardId == dashboardId
                     select hierarchy);
  </code></pre>
  
  <p>When I inspect that query in the debugger it shows the following SQL</p>
  
  <pre><code>SELECT 
  [Extent1].[DashboardId] AS [DashboardId], 
  [Extent1].[CurrentId] AS [CurrentId], 
  [Extent1].[PolygonTypeId] AS [PolygonTypeId], 
  [Extent1].[DisplayName] AS [DisplayName], 
  [Extent1].[ParentId] AS [ParentId]
  FROM [dbo].[PolygonHierarchyView] AS [Extent1]
  WHERE [Extent1].[DashboardId] = @p__linq__0
  </code></pre>
  
  <p>If I run that in SQL Server Management Studio substituding <code>@p__linq__0</code> with the value of <code>dashboardId</code>.  I get these results.</p>
  
  <pre><code>DashboardId     CurrentId  Type  Name       ParentId
  4               5          1     Region     NULL
  4               6          2     Market     NULL
  4               7          3     SubMarket  6
  4               8          4     ZipCode    7
  4               6          2     Market     5
  4               7          3     SubMarket  6
  4               8          4     ZipCode    7
  </code></pre>
  
  <p>However the results from iterating the EF query are as follows.</p>
  
  <pre><code>DashboardId     CurrentId  Type  Name       ParentId
  4               5          1     Region     NULL
  4               6          2     Market     NULL
  4               7          3     SubMarket  6
  4               8          4     ZipCode    7
  4               6          2     Market     NULL
  4               7          3     SubMarket  6
  4               8          4     ZipCode    7
  </code></pre>
  
  <p>Notice that the fifth row has a <code>ParentId</code> of <code>NULL</code> instead of 5.  This is how I worked around the problem.</p>
  
  <pre><code>var hierarchies = (from hierarchy in ctx.PolygonHierarchyViews
                     where hierarchy.DashboardId == dashboardId
                     group hierarchy by hierarchy.ParentId into grp
                     select grp).AsEnumerable();
  </code></pre>
  
  <p>The odd thing here is that this results in a <code>IGrouping</code> with a <code>Key</code> value of 5, but the <code>ParentId</code> of the single object in that group is <code>null</code>.</p>
  
  <p>I'm attempting to creat a <code>lookup</code> from that query and wanted to just do</p>
  
  <pre><code>var lookup = hierarchies.ToLookup(h =&gt; h.ParentId);
  </code></pre>
  
  <p>But since the actually <code>ParentId</code> doesn't seem to always have the correct value and I have to do the group by I end up having to do the following</p>
  
  <pre><code>var lookup = hierarchies.SelectMany(x =&gt; x.Select(y =&gt; new { x.Key, View = y }))
                          .ToLookup(h =&gt; h.Key, h =&gt; h.View);
  </code></pre>
  
  <p>To make matters even stranger, if I remove the <code>AsEnumerable</code> from the end of the query before doing the <code>SelectMany</code> and <code>ToLookup</code> it will still result in the entity that should have a <code>ParentId</code> of 5 being grouped under <code>null</code>.</p>
  
  <p>Is this some type of bug with EF or am I just missing something here?  BTW I'm using EF 6.1.3.</p>