Session.Query<Registration>()
    .Where(r => (theDate < r.Start || today <= r.End))
    .OrderBy(r => r.Start)
    .FetchMany(r => r.People)
    .ThenFetch(p => p.Location) //optional
    .ToList();

2014-12-05 20:48 GMT+01:00 Steve Brown <[email protected]>:

> I have an entity model along the lines of the following:
>
>     public class Registration
>     {
>         public virtual int Id { get; set; }
>         public virtual DateTime Start { get; set; }
>         public virtual DateTime End { get; set; }
>         public virtual IList<Person> People { get; set; }
>     }
>
>     public class Person
>     {
>         public virtual int Id { get; set; }
>         public virtual Registration Registration { get; set; }
>         public virtual string Name { get; set; }
>         public virtual Location Location { get; set; }
>     }
>
>     public class Location
>     {
>         public virtual int Id { get; set; }
>         public virtual string Description { get; set; }
>     }
>
> Currently, the Registration.People collection is lazy loaded, and the
> Person.Location property is fetched with a join when loading a Person
> entity.
>
> I have a view that needs to load all registration entities on a particular
> day.  I have created the query easily enough and it returns the expected
> results:
>
> DateTime theDate = DateTime.Today;
> Session.Query<Registration>()
>     .Where(r => (theDate < r.Start || today <= r.End))
>     .OrderBy(r => r.Start)
>     .ToList();
>
> However with my existing model, NHibernate issues a query to fetch the
> Registration entities, then* for each Registration found*, issues another
> query to fetch the Person entities.  While this certainly works, it results
> in a lot of queries if there are a lot of Registration records on a
> particular day.
>
> I'm wondering if there is a more efficient way to fetch the child
> records?  For example, if I were stuck using ADO.NET and building my
> queries by hand, I might query all the Registration records, collect the
> Ids of the returned entities, then issue a single query for Person entities
> with Registration Ids in the collection, then map the Person entities back
> to the appropriate Registration record.  I've looked at Futures and eager
> fetching the children, but those produced ugly results (like Cartesian
> result sets, etc.).  Can I accomplish something similar with NHibernate?
>
> TIA,
> Steve
>
> --
> You received this message because you are subscribed to the Google Groups
> "nhusers" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/nhusers.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/d/optout.

Reply via email to