理解 Select 操作 | Azure Cosmos DB Table API

Understanding Select operation | Azure Cosmos DB Table API

IList<string> columns = new List<string> { "PartitionKey", "GroupId" };

TableQuery<Entity> rangeQuery = new TableQuery<Entity>()
    .Select(columns)
    .Where(
        TableQuery.CombineFilters(
            TableQuery.GenerateFilterCondition("GroupId", QueryComparisons.GreaterThan, "5555"),
            TableOperators.And,
            TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.GreaterThan, "4")
        )
    )
    .OrderBy("PartitionKey")
    .Take(10);
    
foreach (Entity e in table.ExecuteQuery(rangeQuery))
{
    Console.WriteLine(e.PartitionKey + " " + e.RowKey + " " + e.GroupId);
}

table 有 3 列 - PartitionKey、RowKey 和 GroupId。我正在尝试投影 PartitionKey 和 GroupId 列。

预期行为:Console.WriteLine 应该为 columns 打印正确的值,为 e.RowKey 打印一些默认值,或者可能有一些错误。

观察到的行为:e.RowKey 值被打印为存储在 Table。

在链中,我尝试了 Select 的不同位置,但没有任何区别。我缺少什么理解?与 lazy/deferred 执行有关吗?

PartitionKeyRowKey 是默认列,即使未在 selected 列中指定,它们也始终包含在查询中。

这可以通过从列集合中也删除分区键来验证,即使结果也应该有 PartitionKey

如果您不想在默认情况下将系统属性包含在 select 列中,请选中 this

默认情况下,将返回 3 个系统属性 partitionkeyrowkeytimestamp

如果你不想获取它们,你应该在ExecuteQuery方法中指定ProjectSystemProperties=false注意:列在IList<string> columns = new List<string> { "PartitionKey", "GroupId" } 还是可以退的)。

这是一个示例,您可以修改它以满足您的要求:

        IList<string> columns = new List<string> { "PartitionKey", "PhoneNumber" };

        TableQuery<CustomerEntity> rangeQuery = new TableQuery<CustomerEntity>()
            .Select(columns)
            .Where(
                TableQuery.CombineFilters(
                    TableQuery.GenerateFilterCondition("PhoneNumber", QueryComparisons.Equal, "9999"),
                    TableOperators.And,
                    TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, "r11")
                )
            )
            .OrderBy("PartitionKey")
            .Take(10);


        //here, specify ProjectSystemProperties=false, so Rowkey will not be returned since it's not defined in the "IList<string> columns" above.
        foreach (CustomerEntity e in table.ExecuteQuery(rangeQuery, new TableRequestOptions() { ProjectSystemProperties=false}))
        {
            Console.WriteLine("PartitionKey: " +e.PartitionKey + ", RowKey: " + e.RowKey + ", PhoneNumber: " + e.PhoneNumber);
        }

测试结果: