Linq Queries over WCF
I would like to design my application to do the following: 1. Send a LINQ 开发者_C百科query from a client to some WCF service on some server 2. That server receives that LINQ query and performs a linq-to-object search 3. The server then returns back the results.
Is this possible? If so how? I'm unable to find any tutorials on this topic.
Note: I do not want to send a method-call to a WCF service which then performs an underlying LINQ query. I would like the WCF service to receive some form of a custom LINQ query and compute it.
Linq is merely delegate methods performing operations on collections of objects. So if your client is aware of the types that your server should process, then you should be able to create and pass Func<T>
objects to the server for processing on its object collections. I have done that once. Just remember that your client should be aware of anything that your server would end up having in the linq query.
Unless you want the client to be able to construct linq querys from scratch, then I don't see any good way. A few bad ones, but I'm not going there.
As a totally fictitious example, you can do like this:
Func<Order,bool> filter = o => o.Price > 0;
// pass the filter object to the server and on server do:
var res = objectCollection.Where(filter);
Provided your objects are of type Order and contain a property called "Price", the query would in this case return all orders that cost something. As you also can see, your client has to know about the type "Order" in order to set up the query.
If Linq query (from client) is your primary importance then why not use WCF Data Services; it support linq operation from client.
ashraf
One plain simple way will be sending the LINQ query as a string to the service method with its return type with your appropriate object. So the service will simply execute the query and return you the serialised object (might be).
But do you really looking forward to have a generic method to return a set of LINQ queries?
I am not sure can write linq on the fly... someone correct me if I am wrong.
A better way would be to use dataadaptors and just pass the sql to it and return the dataset.
精彩评论