title: Cannot cast OneTimeEnumerable to System.Collections.Generic.List
body: <p>So, I'm currently working on a small client application with an MSSQL database backend. </p>
  
  <p>I've been making use of Linq to SQL, and therein <code>CompiledQuery</code>. However, when retrieving a list of objects from the database, I'm getting an <code>InvalidCastException</code> with the message:</p>
  
  <pre><code>Unable to cast object of type 'OneTimeEnumerable`1[DiallerBlacklistManager.Data.Entities.BlacklistDataContext.BlacklistEntry]' to type 'System.Collections.Generic.List`1
  </code></pre>
  
  <p>This is the first time I've ever encountered this error, and I'm a little at a loss as to what it is referring to. </p>
  
  <p>My compiled query Func looks like so:</p>
  
  <pre><code>    public static Func&lt;BlacklistDataContext, DateTime, DateTime, List&lt;BlacklistEntry&gt;&gt;
          GetBlacklistEntriesByBlacklistDateRangeFunc =
              CompiledQuery.Compile&lt;BlacklistDataContext, DateTime, DateTime, List&lt;BlacklistEntry&gt;&gt;(
                  (db, startTime, endTime) =&gt;
                      (from BlacklistEntry e in db.BlacklistEntries
                          where e.BlacklistDate &gt;= startTime &amp;&amp; e.BlacklistDate &lt;= endTime
                          select e).ToList());
  </code></pre>
  
  <p>And the method that calls it:</p>
  
  <pre><code>    public List&lt;BlacklistEntryDisplayItem&gt; DoSearch(string searchText, DateTime startTime, DateTime endTime,
          bool includeDeleted)
      {
          List&lt;BlacklistEntryDisplayItem&gt; output = new List&lt;BlacklistEntryDisplayItem&gt;();
          if (!string.IsNullOrEmpty(searchText))
          {
              output.AddRange(GetBlacklistEntriesBySearchAndDateRange(searchText, startTime, endTime).Select(be =&gt; (BlacklistEntryDisplayItem) be));
              if (includeDeleted)
              {
                  output.AddRange(
                      GetDeletedBlacklistEntriesBySearchAndDateRange(searchText, startTime, endTime)
                          .Select(be =&gt; (BlacklistEntryDisplayItem) be));
              }
          }
          else
          {
              output.AddRange(GetBlacklistEntriesByDateRange(startTime, endTime).Select(be =&gt; (BlacklistEntryDisplayItem)be));
              if (includeDeleted)
              {
                  output.AddRange(
                      GetDeletedBlacklistEntriesByBlacklistDateRange(startTime, endTime)
                          .Select(be =&gt; (BlacklistEntryDisplayItem) be));
              }
          }
          return output;
      }
  </code></pre>
  
  <p>Note that <code>BlacklistEntryDisplayItem</code> is a class used for displaying both <code>BlacklistEntry</code> and <code>DeletedBlacklistEntry</code>in a single type, for tidiness. (It was specified that deleted entries must be stored in a separate table)</p>
  
  <p>In case it is seen as relevant, the <code>BlacklistEntryDisplayItem</code> class is:</p>
  
  <pre><code>public class BlacklistEntryDisplayItem
  {
      public BlacklistEntryDisplayItem(int id, string phoneNumber, DateTime blacklistDate, string comments, int userId, bool deleted)
      {
          Id = id;
          PhoneNumber = phoneNumber;
          BlacklistDate = blacklistDate;
          Comments = comments;
          UserId = userId;
          Deleted = deleted;
      }
  
      public int Id { get; private set; }
  
      public string PhoneNumber { get; private set; }
  
      public DateTime BlacklistDate { get; private set; }
  
      public string Comments { get; set; }
  
      public int UserId { get; private set; }
  
      public bool Deleted { get; set; }
  
      public static implicit operator BlacklistEntryDisplayItem(BlacklistEntry entry)
      {
          return new BlacklistEntryDisplayItem(
              entry.Id, 
              entry.PhoneNumber, 
              entry.BlacklistDate, 
              entry.Comments, 
              entry.UserId, 
              false);
      }
  
      public static implicit operator BlacklistEntryDisplayItem(DeletedBlacklistEntry entry)
      {
          return new BlacklistEntryDisplayItem(
              entry.Id,
              entry.PhoneNumber,
              entry.BlacklistDate,
              entry.Comments,
              entry.UserId,
              true);
      }
  }
  </code></pre>
  
  <p>Is anybody able to advise what <code>OneTimeEnumerable</code> is, and why the result set is not of the type defined in the Func type parameters?</p>
  
  <p>Thanks in advance.</p>