Is it possible to create an Expression
that receives a type X
from an Expression
that receives a type Y
if X
contains Y
inside it?
For example, I have these types:
public class Y {
public int Something { get; set; }
}
public class X {
public Y Y { get; set; }
}
Let's say I have this function that returns an Y
Expression
:
private static Expression<Func<Y, bool>> ExprY(Y y2)
{
return y1 => y1.Something == y2.Something;
}
As you can see, I can send a Y
object to this function and it will return an Expression<Func<Y, bool>>
.
Now, let's say that I want to create the same Expression
but from X
, that way I can write this:
private static Expression<Func<X, bool>> ExprX(X x2)
{
return x1 => x1.Y.Something == x2.Y.Something;
}
This works fine, but I am duplicating code since the same logic to compare Something
is inside both functions, so the question is how to rewrite ExprX
to somehow use ExprY
inside itself so I don't need to repeat the Something
comparison?
Something like that:
public static Expression<Func<X, bool>> ExprX(X x2)
{
return x1 => ExprY(x2.Y)(x1.Y);
}
PS. this is intended to be code run by Entity Framework 6 in a .Where
clause, so the answer needs to work with that framework.