Reputation: 2384
I have a web service, that simply returns a lot of data over a method called GetAll()
.
Now what if I dont want all the data? What if I only want a few entities based on a query or some criteria. It feels kind of silly to send all the entities/objects and then handle the "querying" on the consumer side. It's a waste of bandwidth and it's also a waste of time since the query that queries the database for all objects/entities takes quite a while.
Would it be possible to somehow send a lambda-expression or something as an argument and then let the service query the database based on that expression, and then return a list of all the objects back to the consumer?
Upvotes: 0
Views: 1695
Reputation: 1882
No, you can't pass lambdas or delegates in WCF method. For more details see this topic
When I came across your task I used one of this approaches
Dictionary<string,string>
as a container for filter parameters. In this case, on the server side you need to parse values if you have enums, guids, etc. Also it has limitations if we have multiple values for filter parameter. But no need to create a separate class.Upvotes: 0
Reputation: 20364
It might not be a solution for you depending on how your project is set up, but you might want to look into OData Web Services.
http://www.dotnetexpertguide.com/2012/03/odata-service-with-asp-net-web-api.html
If you return your data as IQueryable<T>
Then you can basically pass filters into the URL to return the data which you need.
Upvotes: 1
Reputation:
AFAIK - A lambda isn't going to be serializable so cannot be sent across the wire.
Your question hints at some very bad design decisions however, for instance unrestricted result sets are very bad. You should be looking at using a filter object or some other thing to add predicates to the query. At the least you should think about doing something like:
GetAll(int start, int skip)
Upvotes: 0
Reputation: 14108
It might not be a good idea to do this. It's better to provide the methods the client needs, and if necessary, the client can filter further. But then, I'm not one to judge your question, so, to quote from this post:
At best you could probably get it to accept a serialized version of an Expression, but not a lambda, lambda is a method pointer, an expression is a representation of something that can be compiled, analyzed, etc.
Upvotes: 0