如何使用“SelectMany”使用 DataServiceQuery<>

发布于 2024-08-21 14:12:43 字数 837 浏览 6 评论 0 原文

我有以下 DataServiceQuery 运行 ADO 数据服务(安装了更新以使其像 .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)单一资源

据我所知,我需要使用包含附加 lambda 表达式的“SelectMany”版本(http://msdn. microsoft.com/en-us/library/bb549040.aspx),但我无法使其正常工作。

有人可以告诉我如何正确构建“SelectMany”调用吗?

感谢您的帮助。

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>;

When I run it, I get an exception: Cannot specify query options (orderby, where, take, skip) on single resource

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.

Could someone show me how to properly structure the "SelectMany" call?

Thank you for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你对谁都笑 2024-08-28 14:12:43

数据服务不支持将 SelectMany 与后续 Select 组合在一起,除非您包含一个键选择器来将“Many”过滤回仅一项。

如果您根据 URI 来考虑查询,您就会明白其中的原因。

在 OData URI 中,在导航之前您必须只有一个实体(即 /NavigationProperty)。

所以这样:

~/Users(123)/ConsumerXRef

是可以的,因为在检索许多相关的 ConsumerXRef 之前您有一个 User (123)。

但这并不好:

~/Users(123)/ConsumerXRef/Account

因为在导航到帐户之前您没有识别单个 ConsumerXRef

当您将这种想法带入 LINQ 领域时,像这样:

from u in ctx.Users
where u.ID == 123
from c in u.ConsumerXRef
select c;

是可以的,因为它大致翻译为:

~/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 不是密钥吗?因此,这会转换为类似于此 URL 的内容

~/Users(123)/ConsumerXRef/Account/?$filter=AccountName eq 'MyAccount' ...

,该 URL 无效,因为您在没有先选择特定 ConsumerXRef 的情况下导航(从 ConsumerXRef 到其帐户)。

这有道理吗?

希望如此

亚历克斯

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.

If you think about queries in terms of URIs you will understand why.

In OData URIs you have to have just one Entity before you navigate (i.e. /NavigationProperty).

So this:

~/Users(123)/ConsumerXRef

is okay because you have one User (123) before you retrieve the many related ConsumerXRef(s).

However this is no good:

~/Users(123)/ConsumerXRef/Account

because you don't identify a single ConsumerXRef before navigating to accounts.

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

But this:

from u in _gsc.Users
where u.UserId == myId
from c in u.ConsumerXref
where c.AccountName == "MyAccount" && c.IsActive
select x.Account;

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' ...

which is invalid because you've navigated (from ConsumerXRefs to their Accounts) without first selecting a specific ConsumerXRef.

Does this make sense?

Hope so

Alex

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文