【问题标题】:How to use "SelectMany" with DataServiceQuery<>如何在 DataServiceQuery<> 中使用“SelectMany”
【发布时间】:2010-03-26 18:51:56
【问题描述】:

我有以下 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 表达式 (http://msdn.microsoft.com/en-us/library/bb549040.aspx) 的“SelectMany”版本,但我无法使其正常工作。

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

感谢您的帮助。

【问题讨论】:

  • 我可能错了,因为它是“SelectMany”调用。任何帮助表示赞赏。

标签: wcf-data-services odata


【解决方案1】:

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

如果您根据 URI 来考虑查询,您就会明白为什么。

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

所以这个:

~/Users(123)/ConsumerXRef

没关系,因为在您检索许多相关的 ConsumerXRef(s) 之前,您有一个用户 (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' ...

这是无效的,因为您在未先选择特定 ConsumerXRef 的情况下导航(从 ConsumerXRef 到他们的帐户)。

这有意义吗?

希望如此

亚历克斯

【讨论】:

  • 您的意思是在select x.Account; 中使用c 而不是x
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多