问题
If I had an Object A that contained a many-to-one reference to Object B, where Object B contained a one-to-many collection of Object C... consider the following query:
IQueryable<A> query = getIQueryableSomehow();
List<B> resultList = query.Where(A => A.whatever == something).Select(A => A.B).Fetch(B => B.C).ToList();
I want to do something similar to this, but I keep getting a null reference exception with this code. Is there a sneaky trick to achieve this kind of query AND fetch a bunch of Object B collections, or is it impossible?
Thanks!
回答1:
you can specify Fetch before to load all A,B,C and then select the Bs
List<B> resultList = query
.Where(A => A.whatever == something)
.Fetch(A => A.B).ThenFetch(B => B.C)
.Select(A => A.B)
.ToList();
来源:https://stackoverflow.com/questions/8497073/in-linq-to-nhibernate-is-it-possible-to-use-fetch-after-a-select