【发布时间】:2014-03-19 13:07:06
【问题描述】:
我有一系列由 TableEntity 表示的Reports。它有许多不同的属性,但通常我只需要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);
【问题讨论】:
-
相信查询会返回所有属性。确认这一点的一种简单方法是使用 fiddler 之类的东西来拦截查询和返回的内容。
-
@hocho 我用提琴手测试过,是的,它确认了接受的答案。
标签: c# azure azure-storage azure-table-storage