c# - 使用仅具有所需属性的 TableEntity 是否相当于投影查询?

标签 c# azure azure-storage azure-table-storage

我有一系列由 TableEntity 表示的Report。它有许多不同的属性,但通常,我只需要 ReportID 属性。

public class ReportEntity : TableEntity
{
    public ReportIDEntity()
    {}

    internal ReportIDEntity(Guid reportID, .../*other properties*/)
    {
        ReportID = reportID;
        //assign other properties
    }

    public Guid ReportID { get; set; }

    //other properties
}

我为此编写了一个投影查询,这样我就不必为了获取 ID 而检索整个实体:

const String __ID_COLUMN = "ReportID"; //yuck
IEnumerable<Guid> ids =
_cloudTable_.ExecuteQuery(
new TableQuery<DynamicTableEntity>().Where(
    TableQuery.GenerateFilterCondition(
        "PartitionKey", QueryComparisons.Equal, partitionKey))
    .Select(new[] { __ID_COLUMN }),
    (key, rowKey, timestamp, properties, etag) => properties[__ID_COLUMN].GuidValue.Value);

但是,这非常丑陋(我需要将属性名称指定为字符串才能检索它,并且代码很长)。

如果我创建一个仅具有 ReportID 属性的 TableEntity,并对其进行查询会怎么样? 这会检索所有数据,还是会像投影查询一样精简(带宽方面)?

public class ReportIDEntity : TableEntity
{
    public ReportIDEntity()
    {}

    internal ReportIDEntity(Guid reportID)
    {
        ReportID = reportID;
    }

    public Guid ReportID { get; set; }
}

public class ReportEntity : ReportIDEntity
{
    public ReportEntity()
    {}

    internal ReportEntity(Guid reportID, .../*other properties*/)
     : base(reportID)
    {
        //assign other properties
    }
    //other properties
}

然后查询将是:

IEnumerable<Guid> reportEntities =
_cloudTable_.ExecuteQuery(
new TableQuery<ReportIDEntity>().Where(
    TableQuery.GenerateFilterCondition(
        "PartitionKey", QueryComparisons.Equal, partitionKey)))
.Select(e => e.ReportID);

最佳答案

为了回答您的问题,使用查询投影更有效,因为它是服务器端操作,表服务只会返回仅包含ReportID属性的实体(属性),从而减少网络上的数据流量。当您使用不带投影的查询时,将返回所有属性,并且在反序列化过程中,除了 ReportID 之外的所有其他属性都会在客户端被丢弃。

关于c# - 使用仅具有所需属性的 TableEntity 是否相当于投影查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22489113/

相关文章:

c# - C# 实践练习(学习路径)

c# - 创建一个列表框,其中的项目在被选中时会展开( Accordion )

c# - Azure 存储访问策略,签名不匹配

windows - 如何在不编写自己的程序的情况下将某些文件上传到 Azure Blob 存储?

c# - 将当前应用程序作为单个实例运行并显示上一个实例

c# - 具有流畅验证的集成测试api

在同一物理集群中调整 Azure VM 大小

azure - 将多个产品添加到 Azure API 管理服务上的订阅

azure - 使用 Azure 自动化使用 Powershell 创建云服务实例

c# - 使用带有 sas token 的 Rest api 更新表存储实体显示 "method not allowed"