Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法将OneTimeEnumerable强制转换为System.Collections.Generic.List_C#_.net_Linq To Sql - Fatal编程技术网

C# 无法将OneTimeEnumerable强制转换为System.Collections.Generic.List

C# 无法将OneTimeEnumerable强制转换为System.Collections.Generic.List,c#,.net,linq-to-sql,C#,.net,Linq To Sql,因此,我目前正在使用MSSQL数据库后端开发一个小型客户端应用程序 我一直在使用LINQtoSQL,其中CompiledQuery。但是,当从数据库检索对象列表时,我得到一个InvalidCastException,并显示以下消息: Unable to cast object of type 'OneTimeEnumerable`1[DiallerBlacklistManager.Data.Entities.BlacklistDataContext.BlacklistEntry]' to typ

因此,我目前正在使用MSSQL数据库后端开发一个小型客户端应用程序

我一直在使用LINQtoSQL,其中
CompiledQuery
。但是,当从数据库检索对象列表时,我得到一个
InvalidCastException
,并显示以下消息:

Unable to cast object of type 'OneTimeEnumerable`1[DiallerBlacklistManager.Data.Entities.BlacklistDataContext.BlacklistEntry]' to type 'System.Collections.Generic.List`1
这是我第一次遇到这个错误,我有点不知道它指的是什么

我编译的查询函数如下所示:

    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());
有人能告诉我们什么是
OneTimeEnumerable
,以及为什么结果集不是Func类型参数中定义的类型吗


提前谢谢。

这以前发生在我身上。问题的原因是编译的查询块中的
ToList
。编译后的查询显然只返回
OneTimeEnumerable
,而不是
List
。删除
ToList
,让编译后的查询返回
IEnumerable
,然后在调用编译后的查询后调用
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);
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);
    }
}
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()