Freigeben über


Any/All support in WCF Data Services

The latest CTP of WCF Data Services adds one of the most frequently requested features, namely support for Any/All queries. Prior to this CTP there was no way to filter results based on properties of a related collection (or a multi-value property).

For example if you have this model:

MoviesModel

You couldn’t ask for all movies starring someone called ‘Zack’.

The first thing we did was to add support for Any/All in the OData protocol. If you are interested you can see how the design of Any/All evolved on the OData.org blog via a series of posts first here, then here and finally here.

The latest CTP of WCF Data Services supports this new protocol feature, allowing you to issue requests like this:

~/Movies/?$filter=Actors/any(a: a/Firstname eq ‘Zack’)

Which returns all Movies that have any Actors called ‘Zack’.

Client support

The WCF Data Services client has excellent LINQ support, so we added support for Any/All there too. For example to do the above query you simply write a LINQ query like this:

from m in ctx.Movies
where m.Actors.Any(a => a.Firstname == “Zack”)
select m;

Of course Any/All calls can be nested too, like this:

from m in ctx.Movies
where m.Actors.Any(a => a.Firstname == “Zack” && a.Award.Any())
select m;
Which finds any movies starring any Zack’s who won any awards. Or something like this:

from m in ctx.Movies
where m.Actors.All(a => a.Awards.Any())
select m;

This returns only movies where all of the actors have won an award:

Server support

Any/All is supported in all Data Service providers, i.e. Entity Framework, Reflection Provider and Custom Providers. However the Entity Framework doesn’t support Multi-Value properties yet, so for Entity Framework Any/All queries are limited to just relationships.

If you implement a custom provider in order to add support Any/All there are no API level changes, however you will start need to be able to translate new types of expressions, namely those that include calls to the LINQ Any and All methods. If you can’t do that you can disable Any/All all up by setting DataServiceBehavior.AcceptAnyAllRequests to false.

Summary

As you can see WCF Data Services’ Any/All support greatly improves the query-ability of your OData service, and by adding it we’ve delivered on one of the top community asks. Hopefully you’ll find it very useful too.

You can read more about what is new in the Data Services June 2011 CTP in the Getting Started Guide and Release Notes.

Enjoy!

And of course as always I’m very keen to hear your thoughts.

Alex James
Program Manager
OData Team Microsoft.

Comments

  • Anonymous
    October 25, 2011
    This is good. I can get rid of server methods I expose for the sole reason that there's no Any() on the client.

  • Anonymous
    April 07, 2012
    How can we use them from non .net clients i.e. javascript/REST?

  • Anonymous
    June 09, 2012
    The comment has been removed