如何重构linq表达式树

How to reconstruct linq expression tree

如何检查 linq 表达式树,以便逐个语句重建它(使用 System.Linq.Expressions.Expression 方法)?我正在使用 VS17 中的 DebugView 来可视化表达式,但阅读起来不是很友好。也许有更好的选择?

(免责声明:我是相关图书馆的作者。)

使用 ExpressionTreeToString library, available on NuGet,您可以在表达式上调用 ToString 扩展方法:

// using ExpressionToString
Expression<Func<string, int, string>> expr = (s, i) => $"{s}, {i}";    
Console.WriteLine(expr.ToString("Factory methods"));

并返回如下输出:

// using static System.Linq.Expressions.Expression

Lambda(
    Call(
        typeof(string).GetMethod("Format"),
        Constant("{0}, {1}"), s,
        Convert(i,
            typeof(object)
        )
    ),
    var s = Parameter(
        typeof(string),
        "s"
    ),
    var i = Parameter(
        typeof(int),
        "i"
    )
)

有关 DebugView 属性 使用的语法的详细信息,请参阅 DebugView syntax