我有一个模型版本。其中包含包含文档的一个唯一版本的信息。
在我的 View 模型中,我将其设置如下:
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?
最佳答案
LINQ 查询返回 IQueryable<T>
,而您要求它的类型为 ICollection<T>
。这两个接口(interface)非常不同,您需要将它们归为一性。从技术上来说,需要具体化IQueryable<T>
进入某种内存中的数据序列,这将允许添加和删除其成员(ICollection 接口(interface)抽象的本质)
快速解决方案是添加 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>
,这应该可以正常工作。
关于c# - LINQ 查询拟合模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18728135/