我设立了堆栈溢出OData源作为精彩文章Using LINQPad to Query Stack Overflow概述链接集合的计数和我想要做的事,如:获取使用的OData和LINQ
Users.Where(x=>x.Badges.Count==0).Take(5)
得到网友认为没有徽章(“Badges? We don't need no stinkin' badges!”)。我得到一个DataServiceQueryException:
我设立了堆栈溢出OData源作为精彩文章Using LINQPad to Query Stack Overflow概述链接集合的计数和我想要做的事,如:获取使用的OData和LINQ
Users.Where(x=>x.Badges.Count==0).Take(5)
得到网友认为没有徽章(“Badges? We don't need no stinkin' badges!”)。我得到一个DataServiceQueryException:
不幸的是,OData不支持聚合函数 - 它仅支持有限的一组查询函数described here。
骨料运营
所有聚合操作是不受支持的针对化DataServiceQuery, 包括:
Aggregate Average Count LongCount Max Min Sum
聚合操作必须或者在客户端上执行,或者是 由服务操作包封。
希望微软将在未来增强OData客户端 - 令人沮丧的是(看起来)拥有LINQ的所有能力,然后无法使用它。
看起来像徽章没有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>
可能您需要处理每个用户以检查徽章导航属性是否解析为任何内容。
目前不支持在导航属性中实体的数量过滤(正如上面的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
我开发[AdaptiveLINQ](http://www.adaptivelinq.com):一个组件,可以帮助您将汇总公开为OData Feed – nlips
OData现在支持聚合函数:http://docs.oasis-open.org/odata/odata-data-aggregation-ext/v4.0/odata-data-aggregation-ext-v4 .0.html – Kristopher