Hi,
I have a problem using Linq to NHibernate to load an object and eagerly load a child collection. The objects look like this:
public class Order
{
public Guid Id {get; set; }
public IList<OrderLine> OrderLines {get;set;}
}
public class OrderLine
{
public Guid Id {get;set;}
public string Item {get;set;}
}
I am trying to load an Order with a specific ID and (eagerly) all of it's child OrderLines using Linq. My query looks like this:
using (var s = _sessionFactory.OpenSession())
using (var tx = s.BeginTransaction())
{
var order = from o in s.Linq<Order>().Expand("OrderLines")
where o.Id == id
select o;
return order.First();
}
However, when I display the order, the OrderLines
property only contains one object - the database definitely has 3. Bizarrely, if I do a foreach around order
before the return I do get all 3 child items - but this hits the database twice.
I have tried modifying the query to use Single()
instead, but that doesn't work either.
Am I doing something wrong with linq? Or is my use of Expand
incorrect?
Thanks in advance,
Simon.
Note: I am using FluentNHibernate Automapping to create my NH Mapping, and my database is a Sqlite database (a file, not in memory).