NHibernate无法将类型“System.Collections.Generic.IList<T>”隐式转换为“System.Collections.Generic.IList<IT>
API有一个需要实现的抽象方法:
public IList<IPermission> GetPermissions();
需要注意的是IList<IPermission>这个泛型集合的类型参数IPermission是个接口。
现在我要在实现类中使用NHibernate去实现这个方法,一开始我觉得很简单。
因为有一个实体类Permission实现了IPermission接口,于是很直接的写法:
return NHibernateSession.CreateCriteria(typeof(Permission)).List<Permission>();
编译这段代码,错误:
无法将类型“System.Collections.Generic.IList<Permission>”隐式转换为“System.Collections.Generic.IList<IPermission>”。存在一个显式转换(是否缺少强制转换?)
既然说缺少强制转换,OK。我就将它强制转换一下。
return NHibernateSession.CreateCriteria(typeof(Permission)).List<Permission>() as IList<IPermission>;
这回编译通过, 于是调试,不幸的是,返回结果是null。
解决:
笨方法:
IList<Permission> permissions = NHibernateSession.CreateCriteria(typeof(Permission)).List<Permission>(); IList<IPermission> ipermissions = new IList<IPermission>(); foreach ( Permission permission in permissions )
{
ipermissions.Add(permission);
}
return ipermissions;
保留参考:
.
ipermissions.AddRange((IPermission[])permissions.ToArray()); .
ipermissions.AddRange(permissions.Cast<IPermission>()); .
Converter<Permission, IPermission> converter = delegate(Permission permission) { return permission as IPermission; };
List<IPermission> iPermissions = permissions.ConvertAll<IPermission>(converter);
NHibernate无法将类型“System.Collections.Generic.IList<T>”隐式转换为“System.Collections.Generic.IList<IT>的更多相关文章
- 无法将类型“System.Collections.Generic.List<anonymous type:string ClassID,string ClsssName>”隐式转换为“System.Collections.Generic.List<Ecology.Model.EnergyFlowGraph>”
无法将类型“System.Collections.Generic.List<anonymous type:string ClassID,string ClsssName>”隐式转换为“Sy ...
- using 语句中使用的类型必须可隐式转换为“System.IDisposable
在使用 EF 出现 using 语句中使用的类型必须可隐式转换为“System.IDisposable 今天写在这里分享给大家 出现这样的问题,是因为没有引用 EntityFramework 这个程 ...
- using 语句中使用的类型必须可隐式转换为“System.IDisposable”
在entity framework 中错误 using 语句中使用的类型必须可隐式转换为“System.IDisposable” 的错误. 原因是: 没有引用 EntityFramework 这个程序 ...
- 【.NET】using 语句中使用的类型必须可隐式转换为"System.IDisposable"
#问题: 在使用EF开发中,出现如下错误:“using 语句中使用的类型必须可隐式转换为“System.IDisposable” #原因: 项目中没有引用 EntityFramework 这个程序集: ...
- 报错:无法将类型"System.Data.EntityState"隐式转换为"System.Data.Entity.EntityState"
报错:无法将类型"System.Data.EntityState"隐式转换为"System.Data.Entity.EntityState". 出错语句停留 ...
- 错误 128 无法将类型“string”隐式转换为“System.Windows.Forms.DataGridViewTextBoxColumn”
原因是DataGridView中列的Name属性值和DataPropertyName属性值一样,比如Name="CardID",DataPropertyName="Car ...
- MVC 无法将类型“System.Collections.Generic.List<AnonymousType#1>”隐式转换为“System.Collections.Generic.IList<Mvc3Modeltest.Models.Movie>”。存在一个显式转换(是否缺少强制转换?))
1.问题: 2.解决方案:强制指定类型. 解决之.
- 无法将类型“System.Collections.Generic.IEnumerable<EmailSystem.Model.TemplateInfo>”隐式转换为“System.Collections.Generic.List<EmailSystem.Model.TemplateInf
List<Model.Template> templateList = templateBLL.RecommendTemplateByOrder(modelEbay); List<M ...
- 使用EF6.0出现:CS0029 无法将类型“System.Data.Entity.Core.Objects.ObjectContext”隐式转换为“System.Data.Objects.ObjectContext”错误
这是因为EF6.0重构了一些命名空间后,和VS原有的实体数据模型模板不一致了(ObjectContext context = ((IObjectContextAdapter)dataContext). ...
随机推荐
- Bootstrap入门(二)栅格
Bootstrap入门(二)栅格 Bootstrap入门(二)栅格 全局CSS样式--栅格 先引入本地的CSS文件(根据自己的文件夹,有不同的引入地址,我是放在一个新建的名为css的文件夹中) con ...
- DateTime , DateTime2 ,DateTimeOffset 之间的小区别
闲来无事列了个表比对一下这3兄弟之间还是有一点差距的╮(╯_╰)╭ DateTime DateTime2 DateTimeOffset 日期范围 1753-01-01到 9999-12-31 00 ...
- SqlServer链接MySql操作步骤
Sql Server版本 2008R2 1.从MySQL网站下载最新的MySQL ODBC驱动:http://www.mysql.com/downloads/connector/odbc/,我下载的版 ...
- MySQL-procedure(cursor,loop)
现有一张表spam_keyword,共629条记录,每条记录的word字段的字符数量不等. CREATE TABLE `spam_keyword` ( `kid` ) NOT NULL, `word` ...
- 烂泥:CentOS命令学习之tar打包与解压
本文由秀依林枫提供友情赞助,首发于烂泥行天下. tar命令一般是做打包和解压使用,有关tar命令的使用.我们可以通过帮助文档进行查看,如下: tar –help man tar tar有几个比较重要的 ...
- x01.Lab.StreetApp: MVVM
Store App 特别适用于 MVVM 模式.由于要用到 SQLite,Bing Maps,所以第一步从网上搜索并安装这两个扩展.很难想象在智能手机上运行 SQL Server 这种巨无霸型数据库, ...
- FineReport集成到AWS系统中的方案
本人实施了北京炎黄盈动的BPM及OA系统,主要目标是对业务流程进行控制和管理,加快Oracle JDE的业务前端录单速度和弥补JDE在流程控制方面的不足,实现BPM数据能与JDE无缝互相结合,经过3个 ...
- 挤点时间写博客-php&MySQL实践
hi 晚上要吃火锅的嘛,挤点时间写点东西吧,别被老板发现哦 1.PHP与MySQL 五.文章发布系统之后台 5.2 创建配置文件和初始化文件 为了统一配置以及管理方便,还有就是减少代码的冗余. 分别为 ...
- Core Dump [Linux]
I have known Core-Dump for a long time. But I just know it little. Now, I am about to study it.
- NOIP1999邮票面值设计[搜索|DP]
题目描述 给定一个信封,最多只允许粘贴N张邮票,计算在给定K(N+K≤40)种邮票的情况下(假定所有的邮票数量都足够),如何设计邮票的面值,能得到最大值MAX,使在1-MAX之间的每一个邮资值都能得到 ...