Using
IQueryable<T> query - where T : BaseEntity
I have the following code used in a generic method that uses reflection to call the Where () method (this works):
var predicate = Expression.Lambda(body, item);
MethodInfo whereCall = (typeof(Queryable).GetMethods().First(mi => mi.Name == "Where" && mi.GetParameters().Length == 2).MakeGenericMethod(query.ElementType));
MethodCallExpression call = Expression.Call(whereCall, new Expression[] { query.Expression, predicate });
query = query.Provider.CreateQuery<T>(call);
I would like to use something like this (and avoid reflection):
var predicate = Expression.Lambda<Func<T, bool>>(body, item);
query = query.Where(predicate);
But the problem with this code is that T is used as a base type, not a derived type at runtime.
How can I use T as query.ElementType (derived type)?
source
share