【问题标题】:Can Azure Table Storage Query with projection return just the projected columns and not include the PK and RK?带有投影的 Azure 表存储查询是否可以仅返回投影列而不包括 PK 和 RK?
【发布时间】:2016-09-29 20:41:44
【问题描述】:

我正在编写一个 web.api 控制器,它使用投影查询 Azure 表存储表,并且需要返回投影列的 json 字符串。

结果返回投影,但它还包括每个实体的基本属性 PK、RK、时间戳和 eTag。

我想在屏幕上返回一个仅包含投影列的 json 字符串,但由于包含了基本属性,我必须经过额外的步骤,在序列化为 Json 之前从每个实体中剥离基本属性。

有没有办法让查询只返回投影列?

这是我返回 TableQuery 的代码:

class Poco:TableEntity{
   ... Col1
   ... Col2
}


var rangeQuery = new TableQuery<Poco>().Where(filter).Select(new List<string>
                  { "col1", "col2" });

var result = table.ExecuteQuery(rangeQuery).ToList();

下面是返回 DynamicTableEntity 的相同代码:

class Poco{
   ... Col1
   ... Col2
}


var rangeQuery = new TableQuery();
rangeQuery.Where(filter).Select(new List<string>
                  { "col1", "col2" });

var result = table.ExecuteQuery(rangeQuery).ToList();

这些示例中的每一个都返回基本相同的内容,但在倒置结构中,TableQuery 返回 Poco 元素列表,但每个元素都包含一个“基本”属性,其中包含所有基本属性。 DynamicTableEntity 返回基本属性元素的列表,其中每个元素都包含一个“属性”属性,该属性包含投影列的数组。

【问题讨论】:

    标签: c# azure


    【解决方案1】:

    您可以使用 DynamicTableEntityEntityResolver 查询来查询实体属性的子集,详情请参阅Query a subset of entity properties

    这是从 azure 表中仅获取 NameSchool 的示例代码。

    namespace ProjectedQuery
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Parse the connection string and return a reference to the storage account.
                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
                    CloudConfigurationManager.GetSetting("StorageConnectionString"));
    
                // Create the table client.
                CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
    
                // Create the CloudTable object that represents the "stevens" table
                CloudTable table = tableClient.GetTableReference("steventable");
    
                // Construct the projectionQuery to get only "Name" and "School"
                TableQuery<DynamicTableEntity> projectionQuery = new TableQuery<DynamicTableEntity>().Select(
                    new string[] { "Name", "School" });
    
                // Define an entiy resolver to work with the entity after retrieval
                EntityResolver<SimplePerson> resolver = (pk, rk, ts, props, etag) => new SimplePerson {
                    Name = props["Name"].StringValue,
                    School = props["School"].StringValue
                };
    
    
                foreach (SimplePerson projectedPerson in table.ExecuteQuery(projectionQuery, resolver, null, null))
                {
                    Console.WriteLine("{0}\t{1}", projectedPerson.Name, projectedPerson.School);
                }
    
                Console.Read();
            }
        }
        /// <summary>
        /// The very properties I want to retrive
        /// </summary>
        class SimplePerson
        {
            public string Name { get; set; }
            public string School { get; set; }
        }
    
        /// <summary>
        /// The entity contains all the properties
        /// </summary>
        class PersonEntity:TableEntity
        {
            public string Name { get; set; }
            public string City { get; set; }
            public string School { get; set; }
        }
    }
    

    结果如下:

    【讨论】:

    • 您的回复。这仍然会降低基本属性。我正在做类似的事情,但没有使用 EntityResolver。我在 Select 中创建 Poco 并使用 Dynamic 属性来检索列。这是我的代码:var rangeQuery = new TableQuery(); rangeQuery.Where(filter).Select(new List&lt;string&gt; { "Age", "Nme"}); var result = table.ExecuteQuery(rangeQuery).ToList().Select(x =&gt; new Poco { Age= x.Properties["Age"].Int32Value, Nme= x.Properties["Nme"].StringValue });
    • 关键是结合DynamicTableEntity和EntityResolver,你可以看到SimplePerson是一个普通的类,实际上你可以删除PersonEntity,它是代码中未使用,因此没有基本属性,只有 SimplePerson 中定义的属性。
    • 谢谢,在 TableQuery API 中确实不清楚,也不是用户友好的行为。
    【解决方案2】:

    您可以通过将 DynamicTableEntity 的“属性”属性传递给 Azure 表存储客户端 SDK (> v8.0.0) ConvertBack&lt;T&gt; 方法,将其直接转换为您的业务对象。这里是您的业务对象的类型。

    【讨论】:

      猜你喜欢
      • 2013-03-27
      • 1970-01-01
      • 2014-07-04
      • 2014-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多