无法在 Linq 2 NHibernate 中投影实体

发布于 2024-12-24 22:00:51 字数 491 浏览 6 评论 0原文

我正在 .Net 项目中使用 NHibernate 2,并且使用 Linq2NHibernate 提供程序。 这个简单的查询

var result = from d in session.Linq<Document>()
where d.CreationYear == 2010
select d.ChildEntity).ToList();

抛出一个异常,告诉我不可能将 ChildEntity 类型转换为 Document 类型。 这是为什么? 我还尝试将其翻译为查询方法,难道

session.Linq<Document>()
   .where(d=>d.CreationYear == 2010)
   .select(d=>d.ChildEntity)
   .ToList();

选择方法不应该将 IQueryble 投影到 IQueryble 中,成为 TResult!=T 吗?

I'm working with NHibernate 2 in a .Net project and I'm using the Linq2NHibernate provider.
This simple query

var result = from d in session.Linq<Document>()
where d.CreationYear == 2010
select d.ChildEntity).ToList();

throws an exception telling me that is impossible to cast ChildEntity type do Document type.
Why is that?
I also tried to translate it in query methods, having

session.Linq<Document>()
   .where(d=>d.CreationYear == 2010)
   .select(d=>d.ChildEntity)
   .ToList();

Isn't the select method supposed to project an IQueryble into a IQueryble, beeing TResult!=T ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七颜 2024-12-31 22:00:51

试试这个:

   var result = (from d in session.Linq<Document>()
   where d.CreationYear == 2010
   select new ChildEntityType
     { /* here just do a simple assignments for all ChildEntityType fields
          d.ChildEntity */ } ).ToList();

是的,这可能看起来很愚蠢,但是当您尝试仅选择一个对象时,linq2nhibernate 有时表现得很奇怪。

Try this:

   var result = (from d in session.Linq<Document>()
   where d.CreationYear == 2010
   select new ChildEntityType
     { /* here just do a simple assignments for all ChildEntityType fields
          d.ChildEntity */ } ).ToList();

Yes, this could look quite stupid, but linq2nhibernate sometimes behave very strange, when you try to select just an object.

天气好吗我好吗 2024-12-31 22:00:51

旧的 Linq 提供程序极其有限,并且已经多年无人维护。

我建议您升级到最新的稳定版 NHibernate (3.2),它具有更好(且集成)的 Linq 提供程序。

The old Linq provider is extremely limited and has been unmaintained for several years.

I suggest that you upgrade to the latest stable NHibernate (3.2), which has a much better (and integrated) Linq provider.

匿名。 2024-12-31 22:00:51

你能试试这个吗:

session.Linq<Document>()
   .Where(d=>d.CreationYear == 2010)
   .Select(d=>d.ChildEntity)
   .ToList<T>();     //where T is typeof(ChildEntity)

can you try this:

session.Linq<Document>()
   .Where(d=>d.CreationYear == 2010)
   .Select(d=>d.ChildEntity)
   .ToList<T>();     //where T is typeof(ChildEntity)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文