Home

Convert Linq to Expression Tree Code

Surprised I couldn't find this in the Forum. Trying to decide which edition to buy. Love the Expression Tree visualizer. I need to convert a given Linq to EF Queryable to the c# code necessary to build its Expression Tree. Is that even possible? Can I at least glean the ET components necessary – i.e., Expression.Parameter, Expression.MakeMemberAccess, Expression.Lambda, Expression.Call, etc.? My overriding goal is a deeper dive into ETs than the tutorials I've found specifically to learn how to create complex dynamic queries.

Comments

  • edited September 11

    LINQPad is very useful for this - you can create the query and then dump the query.Expression member as an Expression tree.

    Consider

    var q = OrderLines.Where(o => o.ProductId == 1).Select(o => new { o.InvoiceID, o.LineNumber });
    q.Expression.Dump();
    

    There's also a lot of helpful questions and answers on StackOverflow.

  • That would be a good feature - it would be analogous to what happens when you click 'Tree' (for the Roslyn syntax tree) and then click 'Build with Roslyn Factory Methods'.

    Writing this would be a lot of work, though, unless a library already exists to do the heavy lifting. A quick search of NuGet and github reveals nothing... are you aware of any such project?

Sign In or Register to comment.