0

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?

user7116
  • 63,008
  • 17
  • 141
  • 172
Inx
  • 2,364
  • 7
  • 38
  • 55
  • That depends how your web service is built. If you can alter it or add a method accepting a predicate, it will be easy to achieve what you want. :) – Patryk Ćwiek Jun 27 '12 at 14:23
  • You could pass an encrypted Tuple of McDonald's filet-o-fish sandwiches to your web service if you wanted to. The real question is can your webservice handle it? – Phillip Schmidt Jun 27 '12 at 14:25
  • This seems like something for the ASP.NET Web API, which supports OData and IQueryable on the client. Take a look here: http://stackoverflow.com/questions/10422460/odata-queries-and-types-other-than-iqueryable-in-asp-net-web-api. – Steven Jun 27 '12 at 14:26
  • @Trustme-I'maDoctor Well I have full control of both sides.. and Im just in the process of starting to build the webservice :).. so nothing is really built yet – Inx Jun 27 '12 at 14:31

4 Answers4

1

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.

Tim B James
  • 20,084
  • 4
  • 73
  • 103
0

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.

Community
  • 1
  • 1
Peter
  • 13,733
  • 11
  • 75
  • 122
0

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)

  • Ok, The service aint built yet :).. and I some how need a way to filter the result of the query before it is sent to the client.. and thats where lambda seemed like a good idea.. if it whould have worked :(.. any other suggestions? – Inx Jun 27 '12 at 14:41
0

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

  1. Creating a separate class for filter parameters and pass an instance to a server. On the server side you need to form a query based on passed object.
  2. Usage of a 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.
Community
  • 1
  • 1
FireAlkazar
  • 1,795
  • 1
  • 14
  • 27