Netflix OData with LINQ: "The method 'Select' is not supported."
I'm following a (poor?) example of querying the Netflix catalog by using the following code:
NetflixCatalog cat = new NetflixCatalog(CatalogUri);
IQueryable<Title> query = from person in cat.People
from t in person.TitlesActedIn
where person.Name == searchString
orderby t.ReleaseYear
select new Title
{
Name = t.Name,
BoxArt = t.BoxArt,
开发者_开发百科 Synopsis = t.Synopsis,
ReleaseYear = t.ReleaseYear,
Runtime = t.Runtime,
Type = t.Type,
Genres = t.Genres,
Cast = t.Cast
};
foreach (var title in query)
{
...
}
It's blowing up on the foreach
line with the above-mentioned error.
I think I can give you a query that works, but I can't explain why yours doesn't work (unless you are happy enough with 'Becauses its OData and they don't support every Linq command')
Try changing your query to something like
NetflixCatalog cat = new NetflixCatalog(CatalogUri);
string searchString = "Michael Caine";
var person = (from r in cat.People where r.Name == searchString select r).Single();
var query = (from p in cat.People
where p.Id == person.Id
from t in p.TitlesActedIn
orderby t.ReleaseYear
select new Title
{
Name = t.Name,
BoxArt = t.BoxArt,
Synopsis = t.Synopsis,
ReleaseYear = t.ReleaseYear,
Runtime = t.Runtime,
Type = t.Type,
Genres = t.Genres,
Cast = t.Cast
};
Note, that this is effectively two queries, but I don't think you can combine them into one query. For example You can't just change
where p.Id == persion.Id
to where p.Name == searchString
Now, I'm not sure exactly why, expect that I have learnt that OData is not anything like LinqToSQL (which I am more familar with) and I shouldn't expect it to behave in a similar way.
For example, using linqpad to browse does through up some strange results.
(from r in People where r.Name == "Michael Caine" select r).Single()
returns
Id 13473
Name Michael Caine
Awards Collection<TitleAward> (0 items)
TitlesActedIn Collection<Title> (0 items)
TitlesDirected Collection<Title> (0 items)
which makes it look like he never acted in any films. But
(from r in People where r.Name == "Michael Caine" select new { r.TitlesActedIn } ).Single().Dump();
returns an anonymous class { TitlesActedIn = System.Collections.ObjectModel.Collection`1[LINQPad.User.Title] }
which contains 91 titles.
Whereas
(from r in People where r.Name == "Michael Caine" select r.TitlesActedIn ).Single().Dump();
throws an error : NotSupportedException: Can only specify query options (orderby, where, take, skip) after last navigation.
Hope this helps more than it confuses.
精彩评论