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