We are trying to find a workaround for the issue that the Entity Framework doesn’t support non-scalar entities. We are using a particular equality so we try to build an expression that for a given input and a function from that input checks whether that equality holds.
private static Expression<Func<TElement, bool>> UserEquals<TElement>(User user, Func<TElement, User> select)
{
var userequals = (Expression<Func<User, Boolean>>) (u => u.Source == user.Source && u.UserName == user.UserName);
//return an Expression that receives an TElement, applies |select| and then passes that result to then `userequals` expression
// and uses it's result as return value.
}
I suspect it involves creating a new expression that receives a parameter, but I cannot figure out how to apply the select function to that input and then pass the result of that on to the userequals expression.
The intended usage is something like:
Context.Foo.Where(UserEquals(user, (f => f.User)).Single(f => f.Id == id);
Instead of:
Context.Foo.Single(f => f.Id == id && f.User.Source == user.Source && f.User.UserName == user.UserName);
Ideally we would want to write something like:
Context.Foo.Single(f => f.Id == id && f.User.Equals(user))
// or
Context.Foo.Single(f => f.Id == id && f.User == user)
So, if I’m understanding you correctly you want to do this:
Which would allow you to write:
If you don’t have a base class for accessing the User property of the type Foo you need one such extension for each type, however, that’s something which you could quite easily code gen. I don’t believe expression tree rewriting will be necessary.