Linq to NHibernate - How to return a parent object with only certain child objects included
Posted
by vakman
on Stack Overflow
See other posts from Stack Overflow
or by vakman
Published on 2010-04-28T03:12:18Z
Indexed on
2010/04/28
3:43 UTC
Read the original article
Hit count: 228
nhibernate
|linq-to-nhibernate
Given a simplified model like the following:
public class Enquiry
{
public virtual DateTime Created { get; set; }
public virtual Sender Sender { get; set; }
}
public class Sender
{
public virtual IList<Enquiry> Enquiries { get; set; }
}
How can you construct a Linq to Nhibernate query such that it gives you back a list of senders and their enquiries where the enquiries meet some criteria. I have tried something like this:
return session.Linq<Enquiry>()
.Where(enquiry => enquiry.Created < DateTime.Now)
.Select(enquiry => enquiry.Sender)
In this case I get an InvalidCastException saying you can't cast type Sender to type Enquiry.
Any pointers on how I can do this without using HQL?
© Stack Overflow or respective owner