使用 OData 和 LINQ 获取链接集合的计数

发布于 2024-12-20 19:30:35 字数 465 浏览 1 评论 0原文

我按照精彩文章 使用 LINQPad 查询堆栈溢出,我想做类似的事情:

Users.Where(x=>x.Badges.Count==0).Take(5)

获取没有徽章的用户 ("徽章?我们不需要臭徽章!”)。我收到一个 DataServiceQueryException: 错误快照

I set up the OData feed for Stack Overflow as outlined in the wonderful article Using LINQPad to Query Stack Overflow and I want to do something like:

Users.Where(x=>x.Badges.Count==0).Take(5)

to get the users that have no Badges ("Badges? We don't need no stinkin' badges!"). I get a DataServiceQueryException:
error snapshot

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

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

发布评论

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

评论(3

国产ˉ祖宗 2024-12-27 19:30:36

遗憾的是,OData 不支持聚合函数 - 它仅支持此处描述的有限查询函数集

聚合运算符

DataServiceQuery 不支持所有聚合操作,
包括以下内容:

<前><代码>聚合

平均的

数数

长计数

最大限度

最小

聚合操作必须在客户端上执行,或者在客户端上执行
由服务操作封装。

希望 Microsoft 将来能够增强 OData 客户端 - 令人沮丧的是(看似)拥有 LINQ 的所有功能,但却无法使用它。

Unfortunately, OData doesn't support aggregate functions - it supports only the limited set of querying functions described here.

Aggregate operators

All aggregate operations are unsupported against a DataServiceQuery,
including the following:

Aggregate

Average

Count

LongCount

Max

Min

Sum

Aggregate operations must either be performed on the client or be
encapsulated by a service operation.

Hopefully Microsoft will enhance the OData client in the future - it is frustrating to (seemingly) have all the power of LINQ and then not be able to use it.

第七度阳光i 2024-12-27 19:30:36

看起来 Badges 没有 Count 属性。这就是异常发生的原因。

<EntityType Name="Badge">
    <Key>
    <PropertyRef Name="Id" />
    </Key>
    <Property xmlns:p8="http://schemas.microsoft.com/ado/2009/02/edm/annotation" Name="Id" Type="Edm.Int32" Nullable="false" p8:StoreGeneratedPattern="Identity" />
    <Property Name="UserId" Type="Edm.Int32" Nullable="true" />
    <Property Name="Name" Type="Edm.String" Nullable="true" MaxLength="50" Unicode="true" FixedLength="false" />
    <Property Name="Date" Type="Edm.DateTime" Nullable="true" />
    <NavigationProperty Name="User" Relationship="MetaModel.BadgeUser" FromRole="Badge" ToRole="User" />
</EntityType>

您可能需要处理每个用户以检查徽章导航属性是否解析为任何内容。

Looks like Badges doesn't have a Count property. This is why the exception occurred.

<EntityType Name="Badge">
    <Key>
    <PropertyRef Name="Id" />
    </Key>
    <Property xmlns:p8="http://schemas.microsoft.com/ado/2009/02/edm/annotation" Name="Id" Type="Edm.Int32" Nullable="false" p8:StoreGeneratedPattern="Identity" />
    <Property Name="UserId" Type="Edm.Int32" Nullable="true" />
    <Property Name="Name" Type="Edm.String" Nullable="true" MaxLength="50" Unicode="true" FixedLength="false" />
    <Property Name="Date" Type="Edm.DateTime" Nullable="true" />
    <NavigationProperty Name="User" Relationship="MetaModel.BadgeUser" FromRole="Badge" ToRole="User" />
</EntityType>

Probably you'd need to process each User to check whether the Badges navigation property resolves to anything.

一桥轻雨一伞开 2024-12-27 19:30:36

当前不支持对导航属性中的实体计数进行过滤(如上面 Joe Albahari 已指出的)。在最新的 CTP 中,OData 支持允许您过滤“空”导航属性的任何和所有功能。

请参阅

http://blogs.msdn.com/b/astoriateam/archive/2011/10/13/announcing-wcf-data-services-oct-2011-ctp-for-net-4-and-silverlight-4.aspx to get the latest CTP.

以下是对任意/全部功能的讨论:

http://www.odata.org/blog/even-more-any-and-all

Filtering on count of entities in navigation properties is currently not supported (as already noted by Joe Albahari above). In the latest CTP OData supports any and all functions which would allow you to filter on "empty" navigation properties.

See

http://blogs.msdn.com/b/astoriateam/archive/2011/10/13/announcing-wcf-data-services-oct-2011-ctp-for-net-4-and-silverlight-4.aspx to get the latest CTP.

Here is a discussion of the any/all feature:

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