Is there an easier way to write the expression trees with Predicate Builder. It just seems like a lot of code that can be condensed.
Expression<Func<EventGymCourt, object>> gymCourt = q => q.GymCourt;
Expression<Func<EventGymCourt, object>> gym = q => q.GymCourt.Gym;
Expression<Func<EventGymCourt, object>> address = q => q.GymCourt.Gym.Address;
_eventGymCourtRepository.GetWithStart(page, pageSize, new[] { gymCourt, gym, address }....
Well, I doubt that the types of the
GymCourt,GymandAddressmembers are allobject, so it’s going to be difficult to get type-inference to work in your favour.One way would be to use an array-initializer:
If you really need each expression in a separate variable, you can use a
using-alias to shorten the variable type-declaration.By the way, you appear to be searching for the
Linq.Exprmethod from LINQKit (the library that containsPredicateBuilder). This uses the compiler’s type-inference features to condense the conversion of a lambda to an expression-tree in typical scenarios. I don’t think this is all that useful in your example since you require the expression’s return-type to beobjectrather than the property-type.But assuming you want to create expressions with the property-type as the expression’s return-type, you could have done (quite succinctly):
This lets compiler type-inference work in your favour; the source-code for the method is as simple as:
While that’s not a direct fit for your situation, it’s good to have in your toolkit.