How can I pass a lambda expression to a WCF service?

We have to solve this problem in LINQ-to-Just-About-Everything. For example, when doing LINQ-to-SQL:

var results = from c in customers where c.City == "London" select c.Name;

somehow the content of the lambdas c=>c.City == "London" and c=>c.Name need to end up on the SQL server in a form the server understands. Clearly we cannot persist the lambdas to the server.

Instead what we do is turn the lambdas into Expression Trees, analyze the expression trees at runtime, build an actual string of SQL out of it, and send that string to the server for processing.

You can do the same thing. Create a query language for your server. On the client side, turn the lambdas into expression trees. Analyze them at runtime, turn the result into a string in your query language, and then send the query to the service.

If you’re interested in how this works in LINQ, the LINQ-to-SQL architect Matt Warren has written a long series of blog articles on how to do it yourself:

http://blogs.msdn.com/b/mattwar/archive/2008/11/18/linq-links.aspx

Leave a Comment