I have identical set of conditions that are applied to one class directly or to some other class having same navigation property.
For example, I have home and I have agent, agent is associated with home.
So I am looking for a home with an agency name 'a', or I am looking for agent with name 'a', queries are as follow,
Expression<<Func<Agent,bool>> ax = x=> x.Name == "a" ;
Expression<Func<Home,bool>> hx = x=> x.Agent.Name == "a";
I have nearly 100 search queries for Agent, and I have to also apply them to Home queryable as well. I dont mind writing all again, but its difficult to maintain as we know they will change frequently during course of development.
What I want to do is, I want to compose expression for Home query like this,
Expression<Func<Home,bool>> hx = Combine( x=>x.Agent , x=>x.Name == "a");
Where Combine will be following,
Expression<Func<T,bool>> Combine<T,TNav>(
Expression<Func<T,TNav>> parent,
Expression<Func<TNav,bool>> nav){
// combine above to form...
(x=>x.Agent , x=>x.Name == "a")
=> x => x.Agent.Name == "a"
(x=>x.Agent, x=>x.Name.StartsWith("a") || x.Name.EndsWith("a"))
=> x=>x.Agent.Name.StartsWith("a") || x.Agent.Name.EndsWith("a")
// look carefully, x gets replaced by x.Agent in every node..
// I know very little about expression rewriting, so I need little help
}