1

我在 .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 ?

4

3 回答 3

1

试试这个:

   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 有时会表现得很奇怪。

于 2012-03-22T12:00:06.767 回答
0

你能试试这个:

session.Linq<Document>()
   .Where(d=>d.CreationYear == 2010)
   .Select(d=>d.ChildEntity)
   .ToList<T>();     //where T is typeof(ChildEntity)
于 2012-01-04T11:46:27.367 回答
0

旧的 Linq 提供程序非常有限,并且已经有好几年没有维护了。

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

于 2012-01-04T15:03:09.620 回答