正在使用仅具有所需属性(相当于投影查询)的TableEntity

本文关键字:投影 相当于 查询 TableEntity 属性 | 更新日期: 2023-09-27 17:50:47

我有一系列由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);

正在使用仅具有所需属性(相当于投影查询)的TableEntity

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