how to create Linq Expression from textual representation of the expression RRS feed

  • Question

  • I used Expression.ToString( ) to get the textual representation of a linq expression. Can I/how can I convert that textual representation string back into a Linq Expression?  I want to build a linq expression on a silverlight client, then send that expression up to the server to be executed.


            IQueryable<ListViewRow> query =
              from b in mDataContext.DocMasters
              select new ListViewRow
                DocName = new DocumentName( b.DocName ),
                FormName = b.FormName,
                ScanDateTime = b.ScanDateTime,
                Notes = b.Notes
            System.Linq.Expressions.Expression expr = query.Expression;
            string s5 = expr.ToString();

    Wednesday, February 17, 2010 10:31 PM


  • Hello Steve,


    Welcome to LINQ to SQL forum!


    If I understand the problem correctly, you want to execute exactly the same LINQ to SQL query at the server side, right?   If so, I strongly recommend you consider using this Expression Tree Serialization Library,  


    This library(sample) contains:


    1.     An Expression Tree serialization API: A general purpose XML serialization of Expression Trees. This should work over any expression tree – though there are inevitably bugs. The serialization format is fairly crude, but has been expressive enough to support the variety of expression trees I’ve tried throwing at it.

    2.     A wrapper for serializing/deserializing LINQ to SQL queries: A wrapper around the expression serializer allows serializing LINQ to SQL queries and de-serializing into a query against a given DataContext.

    3.     A WCF service which accepts serialized query expression trees and executes against a back-end LINQ to SQL: To enable querying across tiers, a WCF service exposes service methods which execute serialized queries. The service implementation deserializes the queries against its LINQ to SQL connection.

    4.     An IQueryable implementation wrapping the client side of the WCF service: The client-side calling syntax is simplified by providing an IQueryable implementation. This implementation, RemoteTable, executes queries by serializing the query expression tree and calling the appropriate service. The object model that the service user is able to query against is imported by the WCF service reference per the DataContracts on the LINQ to SQL mapping on the server side.


    Does it fit you scenario?   If you have any questions, please feel free to let me know.


    Have a nice day!



    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Steve Richter Thursday, February 18, 2010 10:04 PM
    Thursday, February 18, 2010 2:04 AM