将 IList 转换为 IList使用动态实例化
我尝试使用动态实例化将以下 NHibernate 查询转换为 IList
IList<AllName> allNames =
(IList<AllName>)Session.CreateQuery(
@"select new AllName(
name.NameId, name.FirstName, origin.OriginId, origin.Description,
name.Sex, name.Description, name.SoundEx
) from Name name join name.Origin origin")
.SetFirstResult(skip)
.SetMaxResults(pageSize);
运行这个我收到以下错误:-
无法转换类型的对象 键入“NHibernate.Impl.QueryImpl” 'System.Collections.Generic.IList`1[Domain.Model.Entities.AllName]'。
我知道我可以返回,
IList results = Sesssion.CreateQuery(...
但我的服务层期望
IList<AllName>
我如何实现这一目标?
I am try to convert the following NHibernate query using dyanmic instantiation into an IList<t> rather than an IList.
IList<AllName> allNames =
(IList<AllName>)Session.CreateQuery(
@"select new AllName(
name.NameId, name.FirstName, origin.OriginId, origin.Description,
name.Sex, name.Description, name.SoundEx
) from Name name join name.Origin origin")
.SetFirstResult(skip)
.SetMaxResults(pageSize);
Running this I get the following error:-
Unable to cast object of type
'NHibernate.Impl.QueryImpl' to type
'System.Collections.Generic.IList`1[Domain.Model.Entities.AllName]'.
I know that I can return
IList results = Sesssion.CreateQuery(...
but my service layers expect an
IList<AllName>
How can I achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种选择是编写一个
IList
实现,它代理IList
,并在适当的情况下进行转换。虽然有点乏味,但做起来应该不会太难。 LINQ 将使实现IEnumerable
等变得稍微容易一些 - 例如,您只需调用底层迭代器并对其调用Cast()
。另一个解决方案是从返回的列表创建一个新的
List
:编辑:正如 Sander 正确指出的那样,这就是
ToList
的用途:编辑:全部可以说,这与 NHibernate 无关——我实际上对 NHibernate 了解不多。 Rippo 现在找到了一个更简单的答案 - 改为调用
List
(这是一个 NHibernate 方法)。One option would be to write an
IList<T>
implementation which proxies to anIList
, casting where appropriate. It shouldn't be too hard to do, albeit somewhat tedious. LINQ will make this slightly easier in terms of implementingIEnumerable<T>
etc - you can just call the underlying iterator and callCast<T>()
on it, for example.Another solution would be to create a new
List<T>
from the returned list:EDIT: As Sander so rightly points out, this is what
ToList
is for:EDIT: All of this has been NHibernate-agnostic, as it were - I don't actually know much about NHibernate. Rippo has now found a much simpler answer - call
List<AllName>
instead (which is an NHibernate method).