Linq查询错误
我正在使用以下 Linq 查询:
from p in People
where p.Name == "George Lucas"
select p.TitlesActedIn
其中 TitlesActedIn 是一个列表。 People 和 TitlesActedIn 关联
但我收到错误:
InvalidCastException:无法将“System.Linq.Expressions.PropertyExpression”类型的对象转换为“System.Data.Services.Client.ResourceExpression”类型。
请提出解决方案。
I am using following Linq query:
from p in People
where p.Name == "George Lucas"
select p.TitlesActedIn
where TitlesActedIn is a list. People and TitlesActedIn are associted
But I am getting error:
InvalidCastException: Unable to cast object of type 'System.Linq.Expressions.PropertyExpression' to type 'System.Data.Services.Client.ResourceExpression'.
Please suggest solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一个非常简单的方法:
需要注意的是,如果您传递的名称不存在,这将会崩溃。 (第一个运算符将抛出异常。您需要保证名称存在,或者分两步完成。
如果您想一步完成,则可以归结为:(请注意返回的内容)
您需要展开,否则它将在
.First()
之后退出计算,因为 TitlesActedIn 将为空。它基本上翻译为选择 Person,包括(展开)TitlesActedIn 关联,然后选择名称(客户端)
做的缺点是,您将从 Titles 表中撤回所有内容(所有字段),因此对于与返回的人员相关的每个标题(标题、年份、描述、简称等)。
这样 在两个查询中,您只能从 TitlesActedIn 关联中拉回“ShortName”。
A very simple way to do it:
Its important to note, that this will crash if the name you pass it does not exist. (The First Operator will throw an exception. You would need to either guarantee that the name exists, or do it in two steps.
If you want to do it in one step it comes down to this:(please note what is coming back)
You need the expand or it will quit evaluating after the
.First()
, because TitlesActedIn will be empty.It basically translates to select the Person, include (expand) the TitlesActedIn association, then select the name (client side)
The downside of this is that you are pulling back everything (all fields) from the Titles table. So for every title associated to the Person it is returning (Title, Year, Description, ShortName, etc).
If you did this in two queries you could only pull back "ShortName" from the TitlesActedIn association.
更新:请参阅问答了解Data Services中Select Many的限制 + 另一种基于$expand的解决方案(注意这需要服务器支持expand)
如果这是WCF Data Services并且TitlesActedIn是一个集合相关电影。
然后,仅当 Person.Name 是主键时,您才可以在一个查询中执行此操作。
为了说明这一点:
将执行您想要的操作,但前提是 Name 是 Person 实体的键,否则不受支持。
如果名称不是关键,那么(今天)执行此操作的一种方法是使用两个查询,如下所示:
另一种选择是进行扩展:
但这依赖于支持 $expand 的服务器 - 并非所有服务器都这样做......
请注意,我们目前正在努力添加 any/all支持 OData 和 WCF 数据服务,一旦发布,您将能够编写:
希望这有帮助
注意:在获取的代码中George Lucas 的关键是我创建了一个匿名类型,因为现在 WCF 数据服务不支持直接物化基元。
UPDATED: See this question and answer to understand the limitations on Select Many in Data Services + Another solution based on $expand (note this requires the server to support expand)
If this is WCF Data Services and TitlesActedIn is a collection of related movies.
Then you can do this in one query only if Person.Name is the primary key.
To illustrate this:
Will do what you want but only if Name is the key of the Person entity, otherwise this is unsupported.
If Name is not the key one way to do this (today) is with two queries, something like this:
Another option though would be do an expand:
But that relies on the server supporting $expand - which not all servers do...
Note we are currently working on adding any/all support to OData and WCF Data Services, once that is released you would be able to write:
Hope this helps
Note: in the code that gets the key for George Lucas I create an anonymous type because today WCF Data Services doesn't support materializing primitives directly.
有趣的是,以下方法
有效:
WCF 客户端自动在 URI 转换中添加扩展调用:
Interestingly, the following works:
as does this:
The WCF client automatically adds the expansion call in the URI translation:
如果我使用 group by 子句和 lambda 表达式来使用 WCF 数据服务获取数据,我会收到类似的错误。我了解到 WCF 数据服务不支持某些操作。请确保您没有使用不受支持的 LINQ 操作。
http://msdn.microsoft.com/en-us/library/ee622463.aspx
I get a similar error if I use a group by clause along with the lambda expression to fetch data using WCF Data Service. I got to know that certain operations are not supported by WCF data services. Please make sure you are not using unsupported LINQ operations.
http://msdn.microsoft.com/en-us/library/ee622463.aspx