简体   繁体   中英

NHibernate: (Fluent) Mapping / Querying based upon a Getter that accesses an already mapped collection property

I don't know how to phrase this properly. I'm working with a pre-existing domain, where certain entities can contain children that are versioned. The children are responsible for their own version number, but ultimately, this version number only makes sense in context of the attached parent entity.

public class Blog
{
    public virtual IList<VersionedItem> VersionedItems { get; set; }
    public virtual CurrentVersionedItem {
        get {
            return VersionedItems.OrderByDescending(x => x.Version).FirstOrDefault();
        }

    }
}

public class VersionedItem
{
    public virtual Blog { get;set; }
    public virtual int Version { get; set; }
    public virtual string Content { get; set; }
    public virtual int SomeNumber { get; set; }
}

And what I'd like to achieve:

var blogs = Session.Query<Blog>(x=> x.CurrentVersionedItem.SomeNumber == 5)

While the IQueryable provider of NHibernate is forgiving, I will not eat everything. Is there a way to define a (fluent) mapping that resolves the "CurrentVersionedItem" property properly?

I'm also aware of the fact CurrentVersionedItem could potentially return null in this scenario (if it worked in the first place).

Why won't you do like this:

var item = session.Query<VersionedItem>().FirstOrDefault(q => q.SomeNumber == 5);
Blog blog;
if (item != null)
    blog = item.Blog;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM