带有投影的Azure表存储查询是否只返回投影列而不包括PK和RK?

时间:2016-05-31 17:46:49

标签: c# azure

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

结果返回投影,但它还为每个实体包括基本属性PK,RK,Timestamp和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元素列表,但每个元素都包含一个包含所有基本属性的“base”属性。 DynamicTableEntity返回一个基本属性元素列表,其中每个元素都包含一个“properties”属性,该属性包含一个预计列的数组。

2 个答案:

答案 0 :(得分:6)

您可以使用 DynamicTableEntity 的查询以及 EntityResolver 查询实体属性的子集,有关详细信息,请参阅Query a subset of entity properties

以下是从azure表中仅获取名称学校的示例代码。

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; }
    }
}

结果如下:

enter image description here

答案 1 :(得分:0)

您可以将DynamicTableEntity直接转换为业务对象,方法是将其“properties”属性传递给Azure Table Storage Client SDK(&gt; v8.0.0)ConvertBack<T>方法。这是您的业务对象的类型。