Using LINQ and Reactive Extensions to overcome limitations in OData query operator
April 11, 2010 3 Comments
I was pleased to know that Netflix had OData API to query. The practical reason is obviously was to use the API to query for the movies I want to watch. Like I mentioned in my previous post, I will be using LINQPad 4 for querying purposes, because of its built-in capabilities for OData as well as for Rx.
One thing I discovered after playing around with OData is that not every query operator in LINQ is available in OData. For example the Netflix API has only for 4 operators which are
And also the query returns only 20 rows as the result for each request. So for example if I have to get 40 rows, on my first request the server would return 20 rows and in my next request I would have to skip first 20 and take next 20 to get 40 rows. These are some of the limitations.
Here is what I wanted from Netflix, I wanted to movie listings that has an average rating greater than 3.5 ,ordered by release year descending and grouped by listings that are available for instant watch. So that I can have one queue for movies that I want to watch online and another one that I can request via mail (the ones that is not available in instant watch). And here is the query to do that
var movies = from counter in (from e in Enumerable.Range(0,400) where e%20 == 0 select e).ToObservable() from movieTitle in Titles.Where (t => t.AverageRating > 3.5).OrderByDescending (t => t.ReleaseYear).Skip(counter).Take(20).ToObservable() select movieTitle; var moviesILikeToWatch = from counter in movies group counter by counter.Instant.Available into g select g; moviesILikeToWatch.Dump();
The first “from counter” query is to build the skip part, like I mentioned by default the result returns only 20 rows I wanted 400 rows to achieve that I used the enumerable range to generate sequence that I can use for skipping in my next query. I could have very well used for loop to build this, but that is not what I want. I want to try and write terse code. These are actual calls to Netflix OData API
http://odata.netflix.com/Catalog/Titles()?$filter=AverageRating gt 3.5&$orderby=ReleaseYear desc&$skip=0&$top=20
http://odata.netflix.com/Catalog/Titles()?$filter=AverageRating gt 3.5&$orderby=ReleaseYear desc&$skip=20&$top=20
In the below picture linqpad makes 20 calls to Netflix for getting 400 movie listings
The next line in the first query “from movieTitle” is simple Linq query to get movies based on filter criteria along with skip and take. The reason for the second query is because the OData API doesn’t provide a groupby operator and if I include it in my first query , Linqpad would try and convert it to OData specific request which would fail. So essentially I am getting all the data from the server and then grouping it locally.
This wouldn’t have been possible without OData.