c# - 超出一定数量的实体后,Azure 表存储上的查询不起作用?

标签 c# linq azure azure-storage azure-table-storage

考虑我的场景。我有大约 200 个分区,每个分区有大约 1000 个行键(实体)甚至更多。因此,当我进行任何查询以获取按字母顺序排列最后一个分区的记录(以“z”开头)时,它不会返回任何结果。

下面是一个示例查询 -

audioRecordServiceContext.QueryableEntities
                         .Where(p => p.PartitionKey == channel && 
                                     p.IsDedication == true && 
                                     p.IsBroadcast == true && 
                                     p.BroadcastTime >= time && 
                                     p.BroadcastTime < time.AddHours(1))
                         .ToList();

当我传递一个以初始字母开头的 channel 时,它会正确返回实体,但是当我给出一个以可能“Z”开头的 channel 时,它不会返回任何实体。

知道如何解决这个问题吗?

编辑:

查询字符串

http://sampleservice/devstoreaccount1/AudioRecord()?$filter=Username eq 'username'

Fiddler 对查询的响应

**HTTP/1.1 200 OK
Cache-Control: no-cache
Transfer-Encoding: chunked
Content-Type: application/atom+xml;charset=utf-8
Server: Windows-Azure-Table/1.0 Microsoft-HTTPAPI/2.0
x-ms-request-id: 06dff157-f693-49a6-ade7-b7165a4d3dfb
x-ms-version: 2009-09-19
x-ms-continuation-NextPartitionKey: 1!16!QWZnaGFuaXN0YW4-
x-ms-continuation-NextRowKey: 1!48!YTZiOGQxZmYtYjNkYy00NDEyLTk2YmItZTViNmUyMWNhYzJi
Date: Wed, 04 Sep 2013 12:19:03 GMT**

最佳答案

下面是一个示例代码,它根据传递的查询来获取所需数量的实体;在我的环境中,这会返回超过 10,000 个实体,并且它会自动处理延续 token 。我不能 100% 确定所使用的 SDK 版本,但它应该适用于 1.7。这里的魔力是由 AsTableServiceQuery 执行的,它是 SDK 提供的一个扩展方法,可以执行分页和自动重试。

_tableName 变量包含我的表的名称。

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConnectionString);
            CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
            TableServiceContext serviceContext = tableClient.GetDataServiceContext();

            var partitionQuery =
                (from e in serviceContext.CreateQuery<MyData1>(_tableName)
                 where e.PartitionKey.CompareTo("15") >= 0 && e.PartitionKey.CompareTo("39") <= 0
                 select new MyData1()
                 {
                     PartitionKey = e.PartitionKey,
                     RowKey = e.RowKey,
                     Timestamp = e.Timestamp,
                     Message = e.Message,
                     Level = e.Level,
                     Severity = e.Severity
                 }
                 ).AsTableServiceQuery();

            return partitionQuery.ToList<MyData1>();

上面的代码依赖于一个名为 MyData1 的类,定义如下:

public class MyData1 : TableServiceEntity
{
    public string Message { get; set; }
    public string Level { get; set; }
    public string Severity { get; set; }
}

希望这有帮助...

关于c# - 超出一定数量的实体后,Azure 表存储上的查询不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18467181/

相关文章:

c# - ASP.NET Web API 抛出异常

c# - Web API 2 - CORS 仅适用于 HTTPS(而非 HTTP)

c# - 相当于休眠中的 ROW_NUMBER() 函数

c# - 将方法转换为 Linq 表达式以供查询

Azure:通过REST API获取单个资源的当前余额

C# - 方法签名中的通用类型 - 为什么是 CS1503, "Cannot Convert from IThing to ns.IThing"?

c# - Visual Studio 2012 项目总是过时(.cs 已修改)

C# Generic List<T> - 如何为每个项目随机分配一个 "Rank"?

android - 将 base64 图像从 Android 发布到 Azure 移动后端

java - 检索 Azure 表中某个分区的所有实体