C# 无法将OneTimeEnumerable强制转换为System.Collections.Generic.List
因此,我目前正在使用MSSQL数据库后端开发一个小型客户端应用程序 我一直在使用LINQtoSQL,其中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
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()