我正在关注使用以下代码查询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
行爆炸。
答案 0 :(得分:4)
我想我可以给你一个有效的查询,但是我无法解释为什么你的工作不起作用(除非你对'确认它的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)。
希望这有助于解决问题。