使用OData和LINQ获取关联集合的计数

3
3个回答

8

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

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.

希望微软未来能够增强OData客户端 - 令人沮丧的是(好像)拥有LINQ的所有功能,却不能使用它。


我开发了AdaptiveLINQ:一个可以帮助您将聚合暴露为OData数据源的组件。 - nlips
2
OData现在支持聚合函数:http://docs.oasis-open.org/odata/odata-data-aggregation-ext/v4.0/odata-data-aggregation-ext-v4.0.html - Kristopher

1

看起来徽章没有计数属性。这就是异常发生的原因。

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

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


1

目前不支持根据导航属性中实体数量进行过滤(正如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.

这是关于 any/all 功能的讨论:
http://www.odata.org/blog/even-more-any-and-all

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接