Silverligth 不支持 Count 和 LongCount,因为它们需要同步执行查询。由于 Silverlight 要求所有网络操作都是异步的,这是不可能的。
您可以不使用 DataServiceContext (或相关类)以编程方式发出有问题的 HTTP 查询,因为 $count 返回数字的文本表示,解析响应并不难。
或者你可以使用一些技巧。您可以使用 IncludeTotalCount() 将 $inlinecount=allpages 查询选项添加到将在响应中包含计数的查询。然后不从服务器下载所有实体,您可以使用 Take(0) 它将添加 $top=0 并因此返回空结果集。但内联计数仍将包含正确的数字。
您可以访问 QueryOperationResponse.TotalCount 属性上的内联计数。像这样的东西:
NetflixCatalog ctx = new NetflixCatalog(new Uri("http://netflix.cloudapp.net/Catalog"));
var q = (DataServiceQuery<Genre>)ctx.Genres.IncludeTotalCount().Take(0);
q.BeginExecute((ar) =>
{
QueryOperationResponse<Genre> r = (QueryOperationResponse<Genre>)q.EndExecute(ar);
r.TotalCount.ToString(); // Use the count in whatever way you need
}, null);