2011-07-01 63 views
0

我有以下NHibernate的LINQ查询,它抛出一个空引用异常.Fetch()为什么会抛出一个空引用异常?

promotions = (from a in session.Query<Application>() 
          from ap in a.Promotions 
          where a.Id == applicationId 
          && ap.EndDate >= DateTime.Now && ap.StartDate <= DateTime.Now 
          select ap).Fetch(ap => ap.LandingPage).ToList(); 

同样的查询,而不.Fetch()工作正常。我两次都传递相同的ID,所以这不是数据问题。

这是一个错误,或通过设计?我怎样才能使它不会抛出异常?

回答

0

如果在声明后立即将.Fetch(ap => ap.LandingPage)移动到更改结果的位置?

from ap in a.Promotions.Fetch(ap => ap.LandingPage) 
+0

促销的类型的ICollection和获取IQueryable的是一个扩展方法,因此它不会编译 –

0
from a in session.Query<Application>().Fetch(ap => ap.LandingPage) 
//the rest of your code 
+0

你一定要明白,他是从不同类型的子表中选择项目,对不对?在这种情况下,你的参数ap不会*有一个名为“LandingPage”的孩子,因为它在ap.Promotions上。 –

相关问题