因此,我目前正在使用带有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
是用于整齐显示单个类型的BlacklistEntry
和DeletedBlacklistEntry
的类。 (已指定删除的条目必须存储在单独的表中)如果认为与之相关,则
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类型参数中定义的类型?提前致谢。
最佳答案
这以前发生在我身上。导致此问题的原因是编译查询块中的ToList
。编译后的查询显然仅返回OneTimeEnumerable<T>
而不是List<T>
。删除ToList
,让您的编译查询返回IEnumerable<BlacklistEntry>
,然后在调用编译查询后调用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()
关于c# - 无法将OneTimeEnumerable强制转换为System.Collections.Generic.List,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28989098/