سؤال

I have a series of Reports represented by a TableEntity. It has many different properties, but often, I only need the ReportID property.

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

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

    public Guid ReportID { get; set; }

    //other properties
}

I've written a projection query for this, so that I won't have to retrieve the entire entity just to get the 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);

However, this is pretty ugly (I need to specify the property name as a string to retrieve it, and the code is long).

What if I make a TableEntity that only has the ReportID property, and query for that? Will this retrieve all of the data, or will it be as lean (bandwidth-wise) as the projection query?

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
}

And then the query would be:

IEnumerable<Guid> reportEntities =
_cloudTable_.ExecuteQuery(
new TableQuery<ReportIDEntity>().Where(
    TableQuery.GenerateFilterCondition(
        "PartitionKey", QueryComparisons.Equal, partitionKey)))
.Select(e => e.ReportID);
هل كانت مفيدة؟

المحلول

To answer your question, using Query Projection is more efficient because it is a server side operation and table service will only return the entities which will only contain ReportID attribute (property) thus less data flows over the network. When you use query without projection, all attributes are returned and except ReportID all other attributes are discarded on the client side during deserialization process.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top