2
votes

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
What is L? Your query makes no sense, not sure what you're asking. - Phill
if L is already instanciated, can't you just go L.C.R? - J. Ed
sJhonny, it's true, I could. I'm trying to understand how this works however if I could not do that because of lazy loading or if didnt have that reference at all or just an Id reference. - Johan
To see what queries NHibernate is running you should either get the NHibernate Profiler (nhprof.com) or in your NHibernate configuration set the show_sql to true, which will output the running SQL in your console. - gcso
Also, seeing your NHibernate mappings might help. - gcso

1 Answers

2
votes

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.