Question

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).

Was it helpful?

Solution

It seems that this feature has a bug:

FirstOrDefault() breaks FetchType=join with Linq to NHibernate

Take a look at the generated database query, if it has TOP 1 clause, this can be the problem.

Remember that Linq to NHibernate is still far from production ready, so this kind of bugs can occur.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top