Convert Expression<Func<TInterface, bool>> to Expression<Func<TImplementation, bool>>
Date : March 29 2020, 07:55 AM
Does that help AFAIK The BCL has very limited support for working with Expressions. I'm afraid that you're going to have to rewrite the expression yourself to change the method parameter type. It's not hard, but not easy either. Basically, you will clone every node of the Expression (it's a tree) but set the root node's data type to your Func .public static Expression<Func<TOut, bool>> CastParam<TIn, TOut>(this Expression<Func<TIn, bool>> inExpr) {
if (inExpr.NodeType == ExpressionType.Lambda &&
inExpr.Parameters.Count > 0) {
var inP = inExpr.Parameters[0];
var outP = Expression.Parameter(typeof(TOut), inP.Name);
var outBody = inExpr.Body.ConvertAll(
expr => (expr is ParameterExpression) ? outP : expr);
return Expression.Lambda<Func<TOut,bool>>(
outBody,
new ParameterExpression[] { outP });
}
else {
throw new NotSupportedException();
}
}
class TInterface { public int IntVal; }
class TImplementation : TInterface { public int ImplVal; }
void Run ()
{
Expression<Func<TInterface, bool>> intExpr = (i => i.IntVal == 42);
Expression<Func<TImplementation, bool>> implExpr = intExpr.CastParam<TInterface, TImplementation> ();
Console.WriteLine ("{0} --> {1}", intExpr, implExpr);
var c = implExpr.Compile ();
Console.WriteLine (c.Invoke (new TImplementation { IntVal = 41, ImplVal = 42 }));
Console.WriteLine (c.Invoke (new TImplementation { IntVal = 42, ImplVal = 41 }));
}
False
True public static Expression Rewrite(this Expression exp, Func<Expression, Expression> c) {
Expression clone = null;
switch (exp.NodeType) {
case ExpressionType.Equal: {
var x = exp as BinaryExpression;
clone = Expression.Equal(Rewrite(x.Left,c), Rewrite(x.Right,c), x.IsLiftedToNull, x.Method);
} break;
case ExpressionType.MemberAccess: {
var x = exp as MemberExpression;
clone = Expression.MakeMemberAccess(Rewrite(x.Expression,c), x.Member);
} break;
case ExpressionType.Constant: {
var x = exp as ConstantExpression;
clone = Expression.Constant(x.Value);
} break;
case ExpressionType.Parameter: {
var x = exp as ParameterExpression;
clone = Expression.Parameter(x.Type, x.Name);
} break;
default:
throw new NotImplementedException(exp.NodeType.ToString());
}
return c(clone);
}
|
Can I use Expression<Func<T, bool>> and reliably see which properties are referenced in the Func<T, bool&
Tag : chash , By : Franky
Date : March 29 2020, 07:55 AM
may help you . Yes, you'll be able to see everything directly referenced. Of course, if someone passes x => ComputeAge(x) > 18
|
How do I convert Expression<Func<T, object>> to Expression<Func<T, bool>>?
Tag : chash , By : itsmegb
Date : March 29 2020, 07:55 AM
I wish this help you Assuming that fieldExpression is something like (T e) => e.Field I concluded that you want find in DB record with same value in Field column. Try this: foreach (T inputRecord in input)
{
object fieldObject = fieldDelegate.Invoke(inputRecord);
ParameterExpression p = fieldExpression.Parameters.First();
// Equivalent to x => x.Field == fieldObject
Expression<Func<T, bool>> predicate = Expression.Lambda<Func<T, bool>>(
// input.Field == fieldObject
Expression.Equal(
// input.Field
fieldExpression.Body,
// constant from fieldObject
Expression.Constant(fieldObject)
),
new []{ p }
);
T dataRecord = dbSet.SingleOrDefault(predicate);
if (dataRecord != null)
{
inputRecord.CreatedOn = dataRecord.CreatedOn;
}
}
|
C# Member expression Func<T,object> to a Func<T,bool> MethodBinaryExpression
Tag : chash , By : Deepak Poondi
Date : March 29 2020, 07:55 AM
around this issue Is it possible to convert a member epxression together with an object to a method binary expression in c#? , You can create just new expression model=>model.property == object.property
public static void SaveBy<T, TProp>(this IDbConnection db, T obj, Expression<Func<T, TProp>> exp) where T : new()
{
var memberExp = (MemberExpression)exp.Body;
var objPropExp = Expression.PropertyOrField(Expression.Constant(obj), memberExp.Member.Name);
var equalExp = Expression.Equal(exp.Body, objPropExp);
var exp2 = Expression.Lambda<Func<T, bool>>(equalExp, exp.Parameters);
//exp2 = {model => (model.prop == value(object).prop)}
if (db.Update(obj, exp2) <= 0)
{
db.Insert(obj);
}
}
|
How to convert Linq Expression<Func<object,object,bool>> to Expression<Func<T1,T2,bool>>
Tag : chash , By : quicky
Date : September 25 2020, 07:00 PM
I hope this helps you . I am trying to store all associations/joins for an ORM in a list of , Here is how you can do it: public class OrmJoin
{
// ...
public Expression AsTyped()
{
var parameters = new[] { Type1, Type2 }
.Select(Expression.Parameter)
.ToArray();
var castedParameters = parameters
.Select(x => Expression.Convert(x, typeof(object)))
.ToArray();
var invocation = Expression.Invoke(Predicate, castedParameters);
return Expression.Lambda(invocation, parameters);
}
public Expression<Func<T1, T2, bool>> AsTyped<T1, T2>() => (Expression<Func<T1, T2, bool>>)AsTyped();
}
void Main()
{
var test = new OrmJoin { Type1 = typeof(string), Type2 = typeof(int), Predicate = (a,b) => Test(a,b) };
var compiled = test.AsTyped<string, int>().Compile();
Console.WriteLine(compiled.Invoke("asd", 312));
}
bool Test(object a, object b)
{
Console.WriteLine(a);
Console.WriteLine(b);
return true;
}
|