问题
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