【问题标题】:Cannot cast OneTimeEnumerable to System.Collections.Generic.List无法将 OneTimeEnumerable 转换为 System.Collections.Generic.List
【发布时间】:2015-03-11 14:18:07
【问题描述】:

所以,我目前正在开发一个带有 MSSQL 数据库后端的小型客户端应用程序。

我一直在使用 Linq to SQL,其中CompiledQuery。但是,当从数据库中检索对象列表时,我收到了 InvalidCastException 的消息:

Unable to cast object of type 'OneTimeEnumerable`1[DiallerBlacklistManager.Data.Entities.BlacklistDataContext.BlacklistEntry]' to type 'System.Collections.Generic.List`1

这是我第一次遇到这个错误,我有点不知道它指的是什么。

我编译的查询 Func 如下所示:

    public static Func<BlacklistDataContext, DateTime, DateTime, List<BlacklistEntry>>
        GetBlacklistEntriesByBlacklistDateRangeFunc =
            CompiledQuery.Compile<BlacklistDataContext, DateTime, DateTime, List<BlacklistEntry>>(
                (db, startTime, endTime) =>
                    (from BlacklistEntry e in db.BlacklistEntries
                        where e.BlacklistDate >= startTime && e.BlacklistDate <= endTime
                        select e).ToList());

以及调用它的方法:

    public List<BlacklistEntryDisplayItem> DoSearch(string searchText, DateTime startTime, DateTime endTime,
        bool includeDeleted)
    {
        List<BlacklistEntryDisplayItem> output = new List<BlacklistEntryDisplayItem>();
        if (!string.IsNullOrEmpty(searchText))
        {
            output.AddRange(GetBlacklistEntriesBySearchAndDateRange(searchText, startTime, endTime).Select(be => (BlacklistEntryDisplayItem) be));
            if (includeDeleted)
            {
                output.AddRange(
                    GetDeletedBlacklistEntriesBySearchAndDateRange(searchText, startTime, endTime)
                        .Select(be => (BlacklistEntryDisplayItem) be));
            }
        }
        else
        {
            output.AddRange(GetBlacklistEntriesByDateRange(startTime, endTime).Select(be => (BlacklistEntryDisplayItem)be));
            if (includeDeleted)
            {
                output.AddRange(
                    GetDeletedBlacklistEntriesByBlacklistDateRange(startTime, endTime)
                        .Select(be => (BlacklistEntryDisplayItem) be));
            }
        }
        return output;
    }

请注意,BlacklistEntryDisplayItem 是一个用于在单一类型中显示 BlacklistEntryDeletedBlacklistEntry 的类,以保持整洁。 (已指定删除的条目必须存储在单独的表中)

如果它被认为是相关的,BlacklistEntryDisplayItem 类是:

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);
    }
}

有人能告诉我OneTimeEnumerable 是什么,为什么结果集不是 Func 类型参数中定义的类型吗?

提前致谢。

【问题讨论】:

    标签: c# .net linq-to-sql


    【解决方案1】:

    这件事以前发生在我身上。问题的原因是您编译的查询块中的ToList。编译后的查询显然只返回OneTimeEnumerable&lt;T&gt; 而不是List&lt;T&gt;。删除 ToList 并让编译后的查询返回 IEnumerable&lt;BlacklistEntry&gt;,然后在调用编译后的查询后调用 ToList

    public static Func<BlacklistDataContext, DateTime, DateTime, IEnumerable<BlacklistEntry>>
        GetBlacklistEntriesByBlacklistDateRangeFunc =
            CompiledQuery.Compile<BlacklistDataContext, DateTime, DateTime, List<BlacklistEntry>>(
                (db, startTime, endTime) =>
                    from BlacklistEntry e in db.BlacklistEntries
                    where e.BlacklistDate >= startTime && e.BlacklistDate <= endTime
                    select e);
    

    然后像这样使用它:

    GetBlacklistEntriesByBlacklistDateRangeFunc(dataContext, startTime, endTime).ToList()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多