NabilS
NabilS

Reputation: 1421

Linq for NHibernate and fetch mode of eager loading

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

Upvotes: 45

Views: 21421

Answers (4)

ilans
ilans

Reputation: 2727

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();

Upvotes: 1

Mike Hadlow
Mike Hadlow

Reputation: 9527

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

Upvotes: 106

Jeff
Jeff

Reputation: 81

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

Upvotes: 8

Paco
Paco

Reputation: 8381

just use it more then once.

IList<Entity> GetDataFromDatabase()
{
    var query = session.Linq<Entity>();
    query.Expand("Property1");
    query.Expand("Property2");
    return query.ToList();
}

Upvotes: 19

Related Questions