c# - Windows Azure 表存储中的批量删除

标签 c# azure azure-table-storage

我正在尝试删除 WADLogsTable 中 2 个日期(即 2 个分区键)之间的所有实体。 到目前为止,我发现的最好的代码是这个(来自 https://www.wintellect.com/deleting-entities-in-windows-azure-table-storage ):

private static void DeleteAllEntitiesInBatches(CloudTable table, Expression<Func<DynamicTableEntity, bool>> filters)
        {
            Action<IEnumerable<DynamicTableEntity>> processor = entities =>
            {
                var batches = new Dictionary<string, TableBatchOperation>();

                foreach (var entity in entities)
                {
                    TableBatchOperation batch = null;

                    if (batches.TryGetValue(entity.PartitionKey, out batch) == false)
                    {
                        batches[entity.PartitionKey] = batch = new TableBatchOperation();
                    }

                    batch.Add(TableOperation.Delete(entity));

                    if (batch.Count == 100)
                    {
                        table.ExecuteBatch(batch);
                        batches[entity.PartitionKey] = new TableBatchOperation();
                    }
                }

                foreach (var batch in batches.Values)
                {
                    if (batch.Count > 0)
                    {
                        table.ExecuteBatch(batch);
                    }
                }
            };

            ProcessEntities(table, processor, filters);
        }

        private static void ProcessEntities(CloudTable table, Action<IEnumerable<DynamicTableEntity>> processor, Expression<Func<DynamicTableEntity, bool>> filters)
        {
            TableQuerySegment<DynamicTableEntity> segment = null;

            while (segment == null || segment.ContinuationToken != null)
            {
                if (filters == null)
                {
                    segment = table.ExecuteQuerySegmented(new TableQuery().Take(100), segment == null ? null : segment.ContinuationToken);
                }
                else
                {
                    var query = table.CreateQuery<DynamicTableEntity>().Where(filters).Take(100).AsTableQuery();
                    segment = query.ExecuteSegmented(segment == null ? null : segment.ContinuationToken);
                }

                processor(segment.Results);
            }
        }

但我不知道应该传递什么作为“filters”参数。 我想出了这个相当天真的尝试:

Expression<Func<DynamicTableEntity, bool>> filters = e => long.Parse(e.PartitionKey) >= startTicks && long.Parse(e.PartitionKey) <= endTicks;

但这行不通。在运行时,我收到以下错误:

The expression ((Parse([10007].PartitionKey) >= 635109048000000000) And (Parse([10007].PartitionKey) <= 635115960000000000)) is not supported.

我不知道我的问题是否与 Azure 表或表达式有关,但我们将不胜感激。

编辑:如果需要,我很乐意提供更多信息。

最佳答案

我在另一个论坛上得到了答案:

var startTicks = string.Format("{0:0000000000000000000}", start);
var endTicks = string.Format("{0:0000000000000000000}", end);
Expression<Func<DynamicTableEntity, bool>> filters = e => e.PartitionKey.CompareTo(startTicks) >= 0 && e.PartitionKey.CompareTo(endTicks) <= 0;

关于c# - Windows Azure 表存储中的批量删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22684881/

相关文章:

c# - Azure B2C 登录然后返回会引发错误

c# - Entity Framework 复合主键-如何获取列的顺序

sql-server - 如何使用参数化数据在 Azure Mobile 中运行 SQL Server 查询

azure - 通过 Azure Active Directory 的 SharePoint 组声明

c# - 绑定(bind)中的Azure函数表存储违反了类型参数 'TElement'的约束

c# - 每次在 PRISM 中发生导航时,如何创建一个新 View ?

c# - 将 DateTime 序列化为二进制

c# - 如何使用安全声明代表 AAD 用户调用 Web API?

azure - windows azure 表存储,如何启用保持事件状态

azure - 如何扩展 SQL Azure?