I've read many great answers here on stack about dynamic creation of lambda expresion, but cannot make my own.
I need to select entity from DB by ID, but I don't know its type beforehand. Only name of a type. So:
var modelType = Assembly.Load("Models").GetTypes().First(t => t.Name == type + "Model");
MethodInfo method = typeof(CommonRepository).GetMethod("GetByExpression",
BindingFlags.Instance | BindingFlags.Public);
var arg = Expression.Constant(null, modelType);
var body = Expression.Convert(Expression.PropertyOrField(arg, "ID"),
typeof(int));
var lambda = Expression.Lambda<Func<object, bool>>(body);
var model = method.Invoke(this, new object[] { lambda });
var field = modelType.GetProperty("Disable", BindingFlags.Instance);
field.SetValue(model, false);
this.marathonRepository.SaveOrUpdate(model);
I guess in my code I've made "e.ID" part of lambda. How to make "== id" part? Thank you for answers.
Aucun commentaire:
Enregistrer un commentaire