如何使用"SelectMany"使用DataServiceQuery<> [英] How to use "SelectMany" with DataServiceQuery<>
问题描述
我有下面的DataServiceQuery反对ADO数据服务运行(已安装更新以使其像.net 4一样运行):
I have the following DataServiceQuery running agaist an ADO Data Service (with the update installed to make it run like .net 4):
DataServiceQuery<Account> q = (_gsc.Users
.Where(c => c.UserId == myId)
.SelectMany(c => c.ConsumerXref)
.Select(x => x.Account)
.Where(a => a.AccountName == "My Account" && a.IsActive)
.Select(a => a)) as DataServiceQuery<Account>;
我在运行它时遇到一个例外:无法在单个资源上指定查询选项(orderby,where,take,skip)
When I run it, I get an exception: Cannot specify query options (orderby, where, take, skip) on single resource
据我所知,我需要使用"SelectMany"版本,其中包括一个附加的lambda表达式( http://msdn.microsoft.com /en-us/library/bb549040.aspx ),但我无法使其正常工作.
As far as I can tell, I need to use a version of "SelectMany" that includes an additonal lambda expression (http://msdn.microsoft.com/en-us/library/bb549040.aspx), but I am not able to get this to work correctly.
有人可以告诉我如何正确组织"SelectMany"呼叫吗?
Could someone show me how to properly structure the "SelectMany" call?
谢谢您的帮助.
推荐答案
Data Services不支持将SelectMany与后续的Select组合在一起,除非您包括一个键选择器以将"Many"过滤回仅一项.
Data Services doesn't support composing SelectMany with a subsequent Select unless you include a key selector to filter the 'Many' back to just one item.
如果您以URI的方式考虑查询,您将理解原因.
If you think about queries in terms of URIs you will understand why.
在OData URI中,在导航之前,您只需拥有一个实体(即/NavigationProperty).
In OData URIs you have to have just one Entity before you navigate (i.e. /NavigationProperty).
所以这个:
~/Users(123)/ConsumerXRef
可以,因为在检索许多相关的ConsumerXRef之前,您有一个用户(123).
is okay because you have one User (123) before you retrieve the many related ConsumerXRef(s).
但是这不好:
~/Users(123)/ConsumerXRef/Account
因为在导航到帐户之前您没有识别出单个ConsumerXRef
.
because you don't identify a single ConsumerXRef
before navigating to accounts.
当您将这种想法带入LINQ领域时,将发生以下情况:
When you take this thinking into LINQ land, something like this:
from u in ctx.Users
where u.ID == 123
from c in u.ConsumerXRef
select c;
可以,因为它可以大致翻译为:
is okay because it roughly translates to:
~/Users(123)/ConsumerXRef
但是这个:
from u in _gsc.Users
where u.UserId == myId
from c in u.ConsumerXref
where c.AccountName == "MyAccount" && c.IsActive
select x.Account;
不好,因为-我猜在这里-AccountName
不是关键吗?所以这会翻译成类似该网址的内容
is no good because - I'm guessing here - AccountName
isn't the key? so this translates to something like this URL
~/Users(123)/ConsumerXRef/Account/?$filter=AccountName eq 'MyAccount' ...
这是无效的,因为您没有先选择特定的ConsumerXRef即可浏览(从ConsumerXRefs到他们的帐户).
which is invalid because you've navigated (from ConsumerXRefs to their Accounts) without first selecting a specific ConsumerXRef.
这有意义吗?
希望如此
亚历克斯
这篇关于如何使用"SelectMany"使用DataServiceQuery<>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!