gpt4 book ai didi

c# - 带有 LINQ : "The method ' Select' is not supported. 的 Netflix OData“

转载 作者:行者123 更新时间:2023-11-30 15:43:09 25 4
gpt4 key购买 nike

我正在关注一个(糟糕的?)使用以下代码查询 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 行爆炸。

最佳答案

我想我可以给你一个有效的查询,但我无法解释为什么你的查询不起作用(除非你对“因为它的 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 

到 其中 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)。

希望这对您有所帮助,而不是造成混淆。

关于c# - 带有 LINQ : "The method ' Select' is not supported. 的 Netflix OData“,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6964324/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com