Entity Framework Count Async Throws Exception
<p>We had a synchrounous call to get a count of disposals that meet certain criteria. The working query looks like this.</p>

<pre><code>            itemsCount = _db.Disposals
                .Include(d =&gt; d.ItemIds)
                .AsEnumerable()
                .Where(d =&gt; d.OrganizationId == SelectedOrganizationID &amp;&amp;
                            d.CreateDateTime.UtcDateTime &gt; greaterThen.ToUniversalTime() &amp;&amp;
                            d.CreateDateTime.UtcDateTime &lt; lessThen.ToUniversalTime())
                .SelectMany(d =&gt; d.ItemIds)
                .Count();
</code></pre>

<p>I needed to make the query async. It seems that I need an IQueryable to run <a href="https://msdn.microsoft.com/en-us/library/dn220197(v=vs.113).aspx" rel="nofollow">CountAsync</a> on it. I don't understand why the code above had <code>.AsEnumerable()</code> in the first place, and why the code below throws an exception:</p>

<blockquote>
  <p>A first chance exception of type 'System.NotSupportedException' occurred in mscorlib.dll</p>
</blockquote>

<pre><code>            itemsCount = await _db.Disposals
                .Include(d =&gt; d.ItemIds)
                .Where(d =&gt; d.OrganizationId == SelectedOrganizationID &amp;&amp;
                            d.CreateDateTime.UtcDateTime &gt; greaterThen.ToUniversalTime() &amp;&amp;
                            d.CreateDateTime.UtcDateTime &lt; lessThen.ToUniversalTime())
                .SelectMany(d =&gt; d.ItemIds)
                .CountAsync();
</code></pre>

<p>I am really just looking for why the async count doesn't work (the project does build, but then the exception is thrown). However, it would be a bonus to understand why <code>AsEnumerable()</code> is used.</p>

<p>P.S. I am aware that it's not running asynchrounously when I put await in front of it, that's just there for testing. <code>_db</code> is the EF database context. </p>

<p>edit: It still throws an exception when written as:</p>

<pre><code>            var greaterThanUtc = greaterThen.ToUniversalTime();
            var lessThanUtc = greaterThen.ToUniversalTime();

            itemsCount = await _db.Disposals
                .Include(d =&gt; d.ItemIds)
                .Where(d =&gt; d.OrganizationId == SelectedOrganizationID &amp;&amp;
                            d.CreateDateTime.UtcDateTime &gt; greaterThanUtc &amp;&amp;
                            d.CreateDateTime.UtcDateTime &lt; lessThanUtc)
                .SelectMany(d =&gt; d.ItemIds)
                .CountAsync();
</code></pre>

<blockquote>
  <p>A first chance exception of type 'System.NotSupportedException'
  occurred in mscorlib.dll</p>
</blockquote>

<p>EDIT 2: </p>

<p>I think the problem is trying convert a Date field in SQL (datetimeoffset) to d.CreateDateTime.UtcDateTime, I am guessing EF doesn't support this at all.</p>