Linq for NHibernate and fetch mode of eager loading
Asked Answered
E

4

45

Is there a way to set the fetchmode to eager for more than one object using linq for nhibernate. There seems to be an expand method which only allows me to set one object. However I need to set it for more than one object. Is this possible? Thanks

Endo answered 6/5, 2009 at 21:13 Comment(0)
O
19

just use it more then once.

IList<Entity> GetDataFromDatabase()
{
    var query = session.Linq<Entity>();
    query.Expand("Property1");
    query.Expand("Property2");
    return query.ToList();
}
Oteliaotero answered 6/5, 2009 at 23:57 Comment(3)
or query.Expand("Property1,Property2");Mooring
Is that an expansion method? Can't find it in NHibernate.Linq v2.0.50727.Welldressed
It is there, on NHibernate.Linq.Query<T>Linen
I
106

The new Linq provider does it a little differently:

var customers = session.Query<Customer>().Fetch(c => c.Orders).ToList();

More here: http://mikehadlow.blogspot.com/2010/08/nhibernate-linq-eager-fetching.html

Igniter answered 6/8, 2010 at 11:22 Comment(2)
if Orders has a sub class of its own, would it load eagerly? if not - how would you change the query to load it?Wedding
To get grandchilds: var customers = session.Query<Customer>() .FetchMany(c => c.Orders) .ThenFetchMany(o => o.OrderLines).ToList();Wedding
O
19

just use it more then once.

IList<Entity> GetDataFromDatabase()
{
    var query = session.Linq<Entity>();
    query.Expand("Property1");
    query.Expand("Property2");
    return query.ToList();
}
Oteliaotero answered 6/5, 2009 at 23:57 Comment(3)
or query.Expand("Property1,Property2");Mooring
Is that an expansion method? Can't find it in NHibernate.Linq v2.0.50727.Welldressed
It is there, on NHibernate.Linq.Query<T>Linen
G
8

As far as I can see, this is not equivalent: SetFetchMode hydrates an objects tree and the Expand method retrieves a cartesian product.

Generally answered 20/9, 2009 at 20:23 Comment(0)
W
1

In contiune to @Mike Hadlow answer, fetching next level (grandchildren) you need to do:

var customers = session.Query<Customer>() .FetchMany(c => c.Orders) .ThenFetchMany(o => o.OrderLines).ToList();

Wedding answered 15/12, 2016 at 13:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.