Q: Using expression returning methods in EF 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?

A: You could create a generic method. The problem is that you need to access properties of unrelated classes. But this is not possible with generics, unless a generic type constraint makes these properties available. Idea: define the required properties in interfaces:
public interface IUserProvider
{
    int UserID { get; set; }
}

public class User : IUserProvider ...
public class Post : IUserProvider ...

Then define the method as
public Expression<Func<TEntity, bool>> GetUserPredicate<TEntity>(int userID)
    where TEntity : IUserProvider
{
    return e => e.UserID == userID;
}


A: Edit:
My solution uses the compiled function of the expression. Since EF only looks at the expression tree and translates it to Sql, it cannot process compiled functions because these are not expression trees (see comment by Filip Corades below).
If you are using my code for other things that can work with compiled functions, you shouldn't have any issues. For this question however it isn't the right answer.
Original answer: 
You can add another method which uses the first one.  
public Expression<Func<Post, bool>> GetPostUserPredicate(int userID)
{
    Expression<Func<User, bool>> expr = GetUserPredicate(userID);
    Func<User, bool> func = expr.Compile();
    return post => func(post.User);
}

What this does is it compiles the function of the user-expression and uses it to make the expression fit for the post. You can call it like this (adjusted to your code):  
var post = dbContext.Posts
        .Where(p => p.ID == 1)
        .Where(GetPostUserPredicate(1))
        .Single();

You could also inline this instead of using an extra function but for me this is a bit too messy. I'll add it anyway:  
var post = dbContext.Posts
    .Where(p => p.ID == 1)
    .Where(p => GetUserPredicate(1).Compile()(p.User))
    .Single();

If there isn't something I'm missing about compiling expressions, this should work absolutely fine.
Hope this helps and let me know if it worked.
