Linq查询错误

| 我正在使用以下Linq查询:
from p in People
 where p.Name == \"George Lucas\"
select p.TitlesActedIn
其中TitlesActedIn是一个列表。与ActedIn相关联的人物和头衔 但我得到了错误:   InvalidCastException:无法将类型\'System.Linq.Expressions.PropertyExpression \'的对象强制转换为\'System.Data.Services.Client.ResourceExpression \'。 请提出解决方案。     
已邀请:
一种非常简单的方法:
var query = People
    .Expand(\"TitlesActedIn\")
    .Where(p => p.Name == \"George Lucas\")
    .First()
    .TitlesActedIn.Select(t => t.ShortName);              
query.Dump();
重要的是要注意,如果您传递的名称不存在,这将会崩溃。 (“第一操作员”将引发异常。您需要保证名称存在,或者分两个步骤进行操作。 如果要一步一步做,就可以做到这一点:(请注意会回来什么)
http://odata.netflix.com/catalog/People()?$filter=Name eq \'George Lucas\'&$top=1&$expand=TitlesActedIn
您需要扩展,否则将在
.First()
之后退出评估,因为TitlesActedIn将为空。 它基本上可以翻译为选择Person,包括(扩展)TitlesActedIn关联,然后选择名称(客户端) 这样做的缺点是您将从Titles表中撤回所有内容(所有字段)。因此,对于与该Person相关的每个标题,它都会返回(标题,年份,描述,短名称等)。 如果您在两个查询中执行了此操作,则只能从TitlesActedIn关联中回退\“ ShortName \”。     
更新:请参阅此问题和答案,以了解“数据服务中选择多个”的局限性+另一个基于$ expand的解决方案(请注意,这需要服务器支持扩展) 如果是WCF数据服务,则TitlesActedIn是相关电影的集合。 然后,只有在Person.Name是主键的情况下,才可以在一个查询中执行此操作。 为了说明这一点:
var titles = from p in people
             where p.Name == \"George Lucas\"
             from m in p.TitlesActedIn
             select m;
仅当Name是Person实体的键时,才可以执行您想要的操作,否则不受支持。 如果“名称”不是关键(今天),一种方法是通过两个查询,如下所示:
var key = (from p in people
          where p.Name == \"George Lucas\"
          select new {p.Id}).Single().Id;

var titles = from p in people
             where p.Id == key
             from m in p.TitlesActedIn
             select m;
另一个选择是进行扩展:
var george = (from p in people.Expand(\"TitlesActedIn\")
             where p.Name == \"George Lucas\"
             select p).Single();

var titles = george.TitlesActedIn;
但这依赖于支持$ expand的服务器-并非所有服务器都支持... 请注意,我们目前正在努力为OData和WCF数据服务添加任何/所有支持,一旦发布,您就可以编写:
var titles = from t in titles
             where t.Actors.Any(a => a.Name == \"George Lucas\")
             select t;
希望这可以帮助 注意:在获得George Lucas密钥的代码中,我创建了一个匿名类型,因为今天WCF Data Services不支持直接实现实例化基元。     
有趣的是,以下作品:
from p in People
where p.Name == \"George Lucas\"
select new { p.TitlesActedIn }
就像这样:
(from p in People
where p.Name == \"George Lucas\"
select new { p.TitlesActedIn }).First().TitlesActedIn
WCF客户端会自动在URI转换中添加扩展调用:
http://odata.netflix.com/Catalog/People()?$filter=Name eq \'George Lucas\'&$top=1&$expand=TitlesActedIn&$select=TitlesActedIn/*
    
如果我将group by子句与lambda表达式一起使用以使用WCF数据服务获取数据,则会遇到类似的错误。我知道WCF数据服务不支持某些操作。请确保您没有使用不支持的LINQ操作。 http://msdn.microsoft.com/en-us/library/ee622463.aspx     

要回复问题请先登录注册