views:

27

answers:

1

Hi, i have two entities one called User and another called Membership which has a one to many mapping from User to Membership. I need to add a property on my User entity called CurrentMembership which gets the latest Membership row (ordered by the property DateAdded on the Membership Entity). I'd appreciate it if someone could show me how this can be done.

Thanks

A: 

I don't think the property needs to be mapped with Fluent NHibernate unless you are planning on storing it in the database, which doesn't necessarily sound like a good idea to me. The following code is more than likely all you need:

public class User
{
    private IList<Membership> _Membership = new List<Membership>();
    public IList<Membership> Memberships 
    {
        get { return _Membership; }
    }

    public Membership CurrentMembership
    {
        get
        { 
            return Memberships
                .OrderByDescending(x => x.DateAdded).FirstOrDefault(); 
        }
    }
}
cbp
Cheers that will work nicely.
nfplee