带有 LINQ 的 Netflix OData:“方法‘Select’”不支持。”

发布于 2024-11-28 14:05:29 字数 925 浏览 0 评论 0原文

我正在遵循一个(糟糕?)使用以下代码查询 Netflix 目录的示例:

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)
{
   ...
}

它在 foreach 行上崩溃并出现上述错误。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毁虫ゝ 2024-12-05 14:05:29

我想我可以给你一个有效的查询,但我无法解释为什么你的查询不起作用(除非你对“因为它的 OData 并且它们不支持每个 Linq 命令”感到足够满意)

尝试将你的查询更改为注意

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
                      };

,这实际上是两个查询,但我认为您不能将它们合并为一个查询。例如你不能只更改

  where p.Id == persion.Id 


where p.Name == searchString

现在,我不确定为什么,期望我已经了解到 OData 与 LinqToSQL(我更熟悉)不同,并且我不应该期望它以类似的方式运行。

例如,使用 linqpad 浏览会出现一些奇怪的结果。

   (from r in People where r.Name == "Michael Caine" select r).Single()

回归

Id             13473 
Name           Michael Caine 
Awards         Collection<TitleAward> (0 items) 
TitlesActedIn  Collection<Title> (0 items) 
TitlesDirected Collection<Title> (0 items) 

这让他看起来好像从未出演过任何电影。

(from r in People where r.Name == "Michael Caine" select  new { r.TitlesActedIn }    ).Single().Dump();

返回一个匿名类
{ TitlesActedIn = System.Collections.ObjectModel.Collection`1[LINQPad.User.Title] }

其中包含 91 个标题。

(from r in People where r.Name == "Michael Caine" select   r.TitlesActedIn ).Single().Dump();

抛出错误:
NotSupportedException:只能在上次导航后指定查询选项(orderby、where、take、skip)。

希望这能有所帮助,而不是令人困惑。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文