开发者

Linq to NHibernate - How to return a parent object with only certain child objects included

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 开发者_C百科I can do this without using HQL?


The Linq provider in NHibernate 2.x is very limited and it usually has problems with projections of entities.

Your query probably works with the included Linq provider in NHibernate 3.x.

HQL is easy:

select Sender
from Enquiry
where Created < current_timestamp

(You can also use a parameter for DateTime.Now)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜