我有一个模型版本。这包含了包含一个独特版本文档的信息。
在我的视图模型中,我将其设置为:
public virtual ICollection<IPACS_Version> rejectionList { get; set; }
所以我现在可以拥有一系列版本文档。
这是LINQ:
model.rejectionList = (from v in db.IPACS_Version
join d in db.IPACS_Document
on v.documentID equals d.documentID
where v.dateRejected != null && (d.createdBy == currUser || d.requester == currUser)
select v);
这给了我以下错误:
无法将类型'System.Linq.IQueryable'隐式转换为'System.Collections.Generic.ICollection'。存在显式转换(您是否错过了演员?)
该行可以返回0到多个“版本”。所以我认为我不能正确理解的是,为什么这个LINQ查询无法适应我的版本模型,因为查询返回0到多个版本?
答案 0 :(得分:2)
LINQ查询返回IQueryable<T>
,而您要求它为ICollection<T>
类型。这两个界面非常独特,您需要将它们融为一体。从技术上讲,你需要将IQueryable<T>
具体化为某种内存中的数据序列,这样就可以添加和删除它的成员(ICollection接口抽象的本质)
快速解决方案是在查询末尾添加ToList
model.rejectionList = (from v in db.IPACS_Version
join d in db.IPACS_Document
on v.documentID equals d.documentID
where v.dateRejected != null && (d.createdBy == currUser || d.requester == currUser)
select v).ToList();
由于IList实现ICollection<T>
,这应该可以正常工作。
答案 1 :(得分:1)
将您的媒体资源定义为IEnumerable<T>
public virtual IEnumerable<IPACS_Version> rejectionList { get; set; }
答案 2 :(得分:1)
如果您无法将视图模型更改为使用IEnummerable<T>
而不是ICollection<T>
,那么解决问题的最简单方法是在最终查询中调用ToList
。 List<T>
实施ICollection<T>
。