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

use*_*142 2 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();
Run Code Online (Sandbox Code Playgroud)

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

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


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

var result = table.ExecuteQuery(rangeQuery).ToList();
Run Code Online (Sandbox Code Playgroud)

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

Ste*_*ven 6

您可以使用DynamicTableEntityEntityResolver的查询查询实体属性的子集,有关详细信息,请参阅查询实体属性的子集.

以下是从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; }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是结果:

在此输入图像描述