开发者

Request Genres by MovieId using LINQ to Netflix OData

I am trying to create a LINQ query to return genres by movieid. The LINQ works in LINQPAD4. Can someone help me with the proper syntax? I am getting the follo开发者_开发知识库wing errors:

Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Linq.IQueryable'. An explicit conversion exists (are you missing a cast?)

and

Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.List'

Code:(note I have wrapped Title in the following line with parenthesis, but are actually brackets in my code.

public List(Genre) GetGenresByMovieId(string movieid)

{

var genres = from t in MovieCatalog.Titles

where t.Id == "BVlLx"

select t.Genres; return genres.ToList();

}


The right query would look like

public IEnumerable<Genre> GetGenresByMovieId(string movieId)
{
    return from title in ctx.Titles
           from genre in title.Genres
           where title.Id == "BVlLx"
           select genre;
}

In the method call syntax, you need to use SelectMany, not Select, since the filter on titles returns a list of titles (which will always contain just one title, but the compiler doesn't know that) and so you want to "concatenate" all genres for each title in the results.

The return type is actually IQueryable, but if you only plan to enumerate over it, you can use IEnumerable, or call ToList() to force execution right there in the method (the way I wrote it the query would actually execute only once you try to enumerate it).


Your problem is your projection:

select new { Name = g.Name }

That is projecting the query into an anonymous type.

You need to project into the IQueryable you have declared (IQueryable<Genre>)

When working with LINQ queries, it's preferable to use implicitly-typed variables (var).

Also, not sure why you have that extra "from" in your query, you don't need that.

Something like this should work:

var genres = from t in MovieCatalog.Titles
             where t.Id = "BVlLx"
             select t.Genres;

return genres.ToList();

var genres should be typed to an IQueryable<Genre>.

That is assuming you want to return a collection of Genre objects.

If you just want the name, do this:

select t.Genres.Name

But that will return a collection of string objects (and var genres should be typed to an IQueryable<string>).

However, i have no idea about the NetFlix OData API, but that should get you on the right track.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜