简体   繁体   中英

NHibernate, QueryOver returning list with list property

I have classes like:

public class A : BaseTableFields
{
    public virtual string Name { get; set; }
    public virtual IList<B> propB { get; set; }
}

public class B : BaseTableFields
{
    public virtual A propA { get; set; }
    public virtual IList<C> propC { get; set; }
}

public class C : BaseTableFields
{
    public virtual B propB { get; set; }
    public virtual IList<D> propD { get; set; }
}

So each of my class has one to many relation to class below of it. How to write the most effecient query, that I receive List of type A (List listOfA) containing records in listOfA.propB and also the listOfA.propB having all of the referencing records in listOfA.propB.propC and so on.

Please help.

Let's assume for the start that:

var list = Session.QueryOver<A>().Where(x=>x.Name == "test").List().ToList();

returns me list with 3 elements of type A, but its property propB is empty.

I would suggest using the 'Fetch' or 'FetchMany' functions in the NHibernate LINQ provider. Examples are in this article for what it actually does and it shows the SQL it will generate.

Working off your example that would result like this:

var list = Session.QueryOver<A>()
                  .Where(x => x.Name == "test")
                  .FetchMany(x => x.propB)
                  .ThenFetchMany(x => x.propC)
                  .ToList();

If this is still causing you issues, then there may be a problem with your mapping files between the one to many relationships of the entities.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM