我已经使用表达式树几天了,我很想知道Expression.Reduce()的作用.在MSDN文档是不是非常有帮助,因为它只是说,它"减少"的表述.为了以防万一,我尝试了一个例子(见下文)来检查这种方法是否包括数学减少,但似乎并非如此.
有谁知道这种方法的作用,是否有可能提供一个快速示例显示它的实际效果?有什么好资源吗?
static void Main(string[] args)
{
Expression<Func<double, double>> func = x => (x + x + x) + Math.Exp(x + x + x);
Console.WriteLine(func);
Expression r_func = func.Reduce();
Console.WriteLine(r_func); // This prints out the same as Console.WriteLine(func)
}
Run Code Online (Sandbox Code Playgroud) public string GetCorrectPropertyName<T>(Expression<Func<T, string>> expression)
{
return ((MemberExpression)expression.Body).Member.Name; // Failure Point
}
Run Code Online (Sandbox Code Playgroud)
并称之为
string lcl_name = false;
public string Name
{
get { return lcl_name ; }
set
{
lcl_name = value;
OnPropertyChanged(GetCorrectPropertyName<ThisClassName>(x => x.Name));
}
}
Run Code Online (Sandbox Code Playgroud)
如果property是string,并且所有其他类型都提供此异常,则此方法正常:
无法将类型为"System.Linq.Expressions.UnaryExpression"的对象强制转换为"System.Linq.Expressions.MemberExpression".
x => x.PropertyName到x => Convert.ToString(x.PropertyName),它仍然失败我哪里错了?
我正在解析表达式树.给定ExpressionType.MemberAccess的NodeType,如何获取该Field的值?
来自C#MSDN文档:MemberAccess是表示从字段或属性读取的节点.
代码片段令人难以置信,非常有用.提前致谢!!!
我的代码看起来像这样:
public static List<T> Filter(Expression<Func<T, bool>> filterExp)
{
//the expression is indeed a binary expression in this case
BinaryExpression expBody = filterExp.Body as BinaryExpression;
if (expBody.Left.NodeType == ExpressionType.MemberAccess)
//do something with ((MemberExpressionexpBody.Left).Name
//right hand side is indeed member access. in fact, the value comes from //aspdroplist.selectedvalue
if (expBody.Right.NodeType == ExpressionType.MemberAccess)
{
//how do i get the value of aspdroplist.selected value?? note: it's non-static
}
//return a list
}
Run Code Online (Sandbox Code Playgroud) Nutshell中的C#有一个名为PredicateBuilder的免费类,它可以在这里逐个构造LINQ谓词.这是该方法的一个摘录,它为谓词添加了一个新表达式.有人可以解释一下吗?(我已经看到了这个问题,我不想要那样的一般性答案.我正在寻找Expression.Invoke和Expression.Lambda如何构建新表达式的具体解释).
public static Expression<Func<T, bool>> And<T> (this Expression<Func<T, bool>> expr1,
Expression<Func<T, bool>> expr2)
{
var invokedExpr = Expression.Invoke (expr2, expr1.Parameters.Cast<Expression> ());
return Expression.Lambda<Func<T, bool>>
(Expression.AndAlso (expr1.Body, invokedExpr), expr1.Parameters);
}
Run Code Online (Sandbox Code Playgroud) 我有一个扩展方法,使用字符串值动态过滤Linq到实体结果.它工作正常,直到我用它来过滤可空列.这是我的代码:
public static IOrderedQueryable<T> OrderingHelperWhere<T>(this IQueryable<T> source, string columnName, object value)
{
ParameterExpression table = Expression.Parameter(typeof(T), "");
Expression column = Expression.PropertyOrField(table, columnName);
Expression where = Expression.GreaterThanOrEqual(column, Expression.Constant(value));
Expression lambda = Expression.Lambda(where, new ParameterExpression[] { table });
Type[] exprArgTypes = { source.ElementType };
MethodCallExpression methodCall = Expression.Call(typeof(Queryable),
"Where",
exprArgTypes,
source.Expression,
lambda);
return (IOrderedQueryable<T>)source.Provider.CreateQuery<T>(methodCall);
}
Run Code Online (Sandbox Code Playgroud)
这是我如何使用它:
var results = (from row in ctx.MyTable select row)
.OrderingHelperWhere("userId", 5);//userId is nullable column
Run Code Online (Sandbox Code Playgroud)
这是我在将可用于可空表列时使用的异常:
没有为类型'System.Nullable`1 [System.Int32]'和'System.Int32'定义二元运算符GreaterThanOrEqual
我无法理解这一点.我该怎么办?
我正在生成一个表达式树,它将属性从源对象映射到目标对象,然后编译为a Func<TSource, TDestination, TDestination>并执行.
这是结果的调试视图LambdaExpression:
.Lambda #Lambda1<System.Func`3[MemberMapper.Benchmarks.Program+ComplexSourceType,MemberMapper.Benchmarks.Program+ComplexDestinationType,MemberMapper.Benchmarks.Program+ComplexDestinationType]>(
MemberMapper.Benchmarks.Program+ComplexSourceType $right,
MemberMapper.Benchmarks.Program+ComplexDestinationType $left) {
.Block(
MemberMapper.Benchmarks.Program+NestedSourceType $Complex$955332131,
MemberMapper.Benchmarks.Program+NestedDestinationType $Complex$2105709326) {
$left.ID = $right.ID;
$Complex$955332131 = $right.Complex;
$Complex$2105709326 = .New MemberMapper.Benchmarks.Program+NestedDestinationType();
$Complex$2105709326.ID = $Complex$955332131.ID;
$Complex$2105709326.Name = $Complex$955332131.Name;
$left.Complex = $Complex$2105709326;
$left
}
}
Run Code Online (Sandbox Code Playgroud)
清理它将是:
(left, right) =>
{
left.ID = right.ID;
var complexSource = right.Complex;
var complexDestination = new NestedDestinationType();
complexDestination.ID = complexSource.ID;
complexDestination.Name = complexSource.Name;
left.Complex = complexDestination;
return left;
}
Run Code Online (Sandbox Code Playgroud)
这是映射这些类型的属性的代码:
public class NestedSourceType
{
public int ID …Run Code Online (Sandbox Code Playgroud) 是否可以查看在Expression树上调用Compile()时生成的IL代码?考虑这个非常简单的例子:
class Program
{
public int Value { get; set; }
static void Main(string[] args)
{
var param = Expression.Parameter(typeof(Program));
var con = Expression.Constant(5);
var prop = Expression.Property(param, typeof(Program).GetProperty("Value"));
var assign = Expression.Assign(prop, con);
Action<Program> lambda = Expression.Lambda<Action<Program>>(assign, param).Compile();
Program p = new Program();
lambda(p);
//p.Value = 5;
}
}
Run Code Online (Sandbox Code Playgroud)
现在,表达式树执行最后一行Main所说的内容.编译应用程序,然后在Reflector中打开它.您可以看到p.Value = 5;执行分配的IL代码.但表达式树是在运行时编译和编译的.是否可以从编译中查看生成的IL代码?
我将如何使用表达式树动态创建类似于......的谓词
(p.Length== 5) && (p.SomeOtherProperty == "hello")
Run Code Online (Sandbox Code Playgroud)
这样我就可以将谓词粘贴到lambda表达式中......
q.Where(myDynamicExpression)...
Run Code Online (Sandbox Code Playgroud)
我只需指向正确的方向.
更新:对不起,我遗漏了一个事实,即我希望谓词具有上述多个条件.对困惑感到抱歉.
例如,如果你有一个这样的表达式:
Expression<Func<int, int>> fn = x => x * x;
Run Code Online (Sandbox Code Playgroud)
是否有任何东西会遍历表达式树并生成它?
"function(x) { return x * x; }"
Run Code Online (Sandbox Code Playgroud) 可能重复:
在c#中组合两个lamba表达式
我有两个以下表达式:
Expression<Func<string, bool>> expr1 = s => s.Length == 5;
Expression<Func<string, bool>> expr2 = s => s == "someString";
Run Code Online (Sandbox Code Playgroud)
现在我需要将它们与OR结合起来.像这样的东西:
Expression.Or(expr1, expr2)
Run Code Online (Sandbox Code Playgroud)
有没有办法使这类似于上面的代码方式:
expr1 || expr2
Run Code Online (Sandbox Code Playgroud)
我理解在这个例子中我可以将它组合在一起:
Expression<Func<string, bool>> expr = s => s.Length == 5 || s == "someString"
Run Code Online (Sandbox Code Playgroud)
但我不能在我的真实代码中这样做,因为我将expr1和expr2作为方法的参数.
c# ×10
expression-trees ×10
linq ×4
.net ×3
lambda ×3
il ×1
javascript ×1
performance ×1
reflection ×1