I had the following code and I want to replace the Department type for a generic type...
I would do like to replace...
Expression.Lambda<Func<Department, bool>>(body, param)
by..
Expression.Lambda<Func<T, bool>>(body, param)
I know that I must use reflection, but all I have is a domain model class boxed as object.
I could get the object and his type name but I am having hard to replace the hard coded type Department by a generic type.
I can't convert method Exists
to Exists<T>
because I don't know what <T>
is at the time of calling the method. I only have the object.
public bool Exists(object id, object source, Type type)
{
var param = Expression.Parameter(type, "e");
var body = Expression.Equal(Expression.Property(param, "Id", Expression.Constant(id));
var where = Expression.Lambda<Func<Department, bool>>(body, param);
var context = new DataContext();
var dbSet = context.Set<Department>();
return dbSet.AsNoTracking().Any(where);
}
For the dbset case I tried the following code
public static IQueryable<object> Set (this DbContext context, Type type)
{
return IQueryable<object>)context.GetType().GetMethod("Set")?.MakeGenericMethod(type).Invoke(context, null);
}
and replace...
var dbSet = context.Set<Department>();
by
var dbSet = context.Set(type);
It seems to work, I got the right dbset but there are some issues with the where clause that gives some run time errors.
What I am trying to achieve is avoid EF Core to do client side evaluation. I.e.
Any(e => e.ToString == id.ToString())
this will work but will perform an undesirable client side evaluation.
Any(e => e.ToString == id.ToString())
? That doesn't compile, and when it would, it wouldn't be a client-side operation at all