views:

569

answers:

1

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

+2  A: 

It seems that this feature has a bug:

http://stackoverflow.com/questions/880061/firstordefault-breaks-fetchtypejoin-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.

bbmud
Thanks, I thought this may have been a bug, but wanted to check that I was using the correct Linq method.Using NHProf I can see that the query has a `limit 1` clause (sqlite equivalent of `top 1`).I have changed my code to be: return order.ToList()[0];and this works as expected.Thanks.
Simon