NHibernate QueryOver

亡梦爱人 提交于 2019-12-11 02:54:57

问题


I've searched around but not found what I am doing wrong here.

I have object C which holds a list of object L, object C also holds a reference to class R.

From object L I want to find object R.

I'm trying to do this but I only get null using this code:

L is already an instanced object a function receives.

var t = SessionController.CurrentSession.QueryOver<C>()
  .Where(c => c.Id == L.C_Id)
  .JoinQueryOver<R>(c => c.R)
  .Select(c => c.R).SingleOrDefault();

Any idea what I'm doing wrong here would be appreciated. Thanks


回答1:


The problem is your SingleOrDefault call is returning an instance of class C and I'm guessing there is not instance of class C that has the Id of the associated class R. You'll want to modify your query as follows:

var t = SessionController.CurrentSession.QueryOver<C>()
  .Where(c => c.Id == L.C_Id)
  .JoinQueryOver<R>(c => c.R)
  .Select(c => c.R).SingleOrDefault<R>();

Note the explicit typing of the SingleOrDefault call.



来源:https://stackoverflow.com/questions/6610531/nhibernate-queryover

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!