I have an expression returning method such as:
public Expression<Func<User, bool>> GetUserPredicate(int userID)
{
return u => u.ID == userID;
}
(In the original code, this is a longer and more complex piece of logic that I want to reuse so that I extrcted it out into such a method.)
I can use it in queries for Users without a problem:
var user = dbContext.Users
.Where(GetUserPredicate(1))
.Single();
But I also want to use it when querying other entities like Posts:
var post = dbContext.Posts
.Where(p => p.ID == 1)
.Where(p.User => GetUserPredicate(1))
.Single();
But this does not work. How can it be accomplished?