I have a WCF service which sits in front of a data collection. I would like to have a service call where the client could pass in a LINQ query, or Expression Tree, to be executed against 开发者_StackOverflow中文版the data collection. Is there a reasonable way to pass a LINQ query to a WCF service call?
Thanks.
The current options for doing this are fairly limited. It is not possible to serialize a full Linq query, pretty much for the same reasons that you cannot serialize arbitrary code and send it somewhere to be executed (of course, you can always dynamically build an assembly, implement an interface and code gen your query... but at that point you've done a ton).
I've written systems a couple times now to allow highly customized searching capabilities over a Linq data source. It essentially comes down to deciding what kinds of queries you need to support. Keep it as simple as possible, and then create a data structure that represents that. If you need full generality then you will start tending towards something that looks like expression trees, but often this is overkill for your purpose.
You then have to write a translator that takes your custom query data structure and creates a Linq query. Using the proper tools (like LinqKit) can make this a surprisingly easy task. But you will have to become pretty much an expert at expression trees and their manipulation.
精彩评论