How to use LINQ expression tree to join to another table?
I'm using the repository pattern with nHibernate at the moment. I'm able to use expression trees without any problems when selecting from just one entity (one table) but I'd like to make an inner join with another table to get a related row. Here's what I've got so far:
public abstract class QueryBase<T>
{
public abstract Expression<Func<T, bool>> MatchingCriteria { get; }
public T SatisfyingElementFrom(IQueryable<T> candidates)
{
return SatisfyingElementsFrom(candidates).SingleOrDefault();
}
public IQueryable<T> SatisfyingElementsFrom(IQueryable<T> candidates)
{
return candidates.Where(MatchingCriteria).AsQueryable();
}
}
public class UserByEmailAddress : QueryBase<User>
{
private string _emailAddress;
public UserByEmailAddress(string emailAddress)
{
_emailAddress = emailAddress;
}
public override Expression<Func<User, bool>> MatchingCriteria
{
get { return user => user.EmailAddress == _emailAddress; }
}
}
I'd like to be able to return a user based on an OrderId. So开发者_如何学编程mething like that:
return user => user.Id == Order.UserId
It's easily done in LINQ but I'm having problems figuring out the expression tree and any help would be much appreciated.
Thanks.
Have a look at:
Loading Subrecords in the Repository Pattern
精彩评论