perhaps not exactly what you're after, but certainly worth considering is the nhibernate.collectionquery library.

it allows you to query uninitialized nhibernate collections on an entity using linq - but would require an additional query/round-trip to get them (unlike fetchmany, which grabs the entire collection in the same round-trip).


you will need a reference from child object to the parent.

var result = from foo in session.query<foo>().fetchmany(f => f.bars)
             from bar in session.query<bar>()
             where == bar.fooid && // fooid is missing in your entity
          > 30
             select foo;


i'm fairly sure you're out of luck with the current linq provider - but for a non-linq (and cross-cutting) option, you might want to have a look at the filter functionality included in nhibernate - it would probably be the best bet for implementing this in a large scale / complex project.


var query = from foo in session.query<foo>()
            where >30
            from bar in foo.bars
            where bar.description.startswith("x")
            select new { id = foo, bar = bar};
var results = query.tolist().tolookup(x => x, x =>;
foreach(var foo in results.keys)
   var barswhichstartwithx = results[foo];
   //do stuff

although this may produce inefficient sql (i don't use nhibernate so i wouldn't know). you could also try this...also the above would miss out foos without bars.

var foosquery = session.query<foo>().where(foo => > 30);
var foos = foosquery.future();
var barsquery = from f in foosquery
                from bar in foo.bars
                select new { id =, bar = bar};
var foosdict = foos.todictionary(x =>;
var bars = barsquery.tolist().tolookup(x => foosdict[], x =>;
foreach(var foo in foos)
   var barswhichstartwithx = bars[foo];
   //do stuff

Related Query

More Query from same tag