my application has the following entity:
public class User
{
public virtual int UserID { get; set; }
public virtual Membership LatestMembership { get { return Membership.First(); } }
public virtual IList<Membership> Membership { get; set; }
public User()
{
Membership = new List<Membership>();
}
}
With the following mapping:
public UserMap()
{
Table("Users");
Id(x => x.UserID);
HasMany(x => x.Membership)
.KeyColumn("UserID")
.OrderBy("DateAdded DESC")
.Inverse()
.Cascade.All();
}
The LatestMembership property against the user simply grabs the first record from the Membership collection (which is ordered so that the newer records are at the top).
So far so good, however now say i want to do the following (i know this will return them all but i’m just using this as an example):
var users = session.Linq<User>()
.Where(u => u.LatestMembership.DateAdded < DateTime.UtcNow);
An error is thrown because the LatestMembership property is beyond the nhibernate linq providers capabilities. The only solution i have so far is to convert it to a list and then apply the where condition but i’d imagine this could become pretty insuficient for a large database.
I was wondering if there was an alternative way i could map this or what your recommendations are. Thanks
Unfortunately, this isn’t really possible using the current Linq provider. One thing you can do is to map the properties of the LatestMembership you want to query on as read-only and use formulas in your mappings to derive the values for it. You can see some further details in my answer to this post.
The other possible solution is to attack this from the
Membershipside of things; query for the latest membership matching the user in question and filter accordingly.