Question

I have this 2 objects:

public class Parent
{
    public virtual int Poid { get; set; }
    public virtual IEnumerable<Child> Child { get; set; }
}

public class Child
{
    public virtual int Poid { get; set; }
    public virtual string Name {get; set;}    
}

I want to use NHibernet QueryOver API to get a child based on the Parent Id and Child Id, That's mean something like give me the child with Id = x belonging to the parent with Id = y.

I tried something like this:

return Session.QueryOver<Parent>().Where(p => p.Poid == y)
                .JoinQueryOver(p => p.WishesLists)
                .Where(c => c.Poid == x)
                .SingleOrDefault<Child>();

But I'm getting an exception that is not possible to convert an object of type Child to Parent.

How is the correct form to QueryOver starting with a Parent Entity but return a Child Entity?

Was it helpful?

Solution

I don't know if this is possible with QueryOver, I worked at it for a while without getting anywhere. It is possible with LINQ:

var child = session.Query<Parent>()
                   .Where(p => p.Poid == y)
                   .SelectMany(p => p.WishesLists)
                   .SingleOrDefault(c => c.Poid == x);

I strongly prefer the LINQ syntax over QueryOver.

See also NH-3176

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