Azure表服务2012年10月版中的投影,不使用字符串来引用该列

时间:2012-11-01 14:55:12

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

在以前版本的表存储API中,我们必须使用上下文和new表达式来处理服务器端的投影以节省带宽:

var result =
    GetContext().CreateQuery<MyEntity>(TABLENAME)
    .Where(...)
    .Select(e => new { e.ColumnToProject }) //convenient to have the real reference to the entity's properties here
    .AsTableServiceQuery().ToList().Select(ee => ee.ColumnToProject);

在2012年10月API的this guide中,microsoft使用带有EntityResolver的DynamicTableEntity来处理投影。语法非常复杂,而且你需要使用字符串来引用列名。

var result =
    tableReference.ExecuteQuery(
        new TableQuery<DynamicTableEntity>()
            .Where(...)
            .Select(new[] {"ColumnToProject"}),
        (key, rowKey, timestamp, properties, etag) => 
            properties["ColumnToProject"].GuidValue) //lets say ColumnToProject contains a guid in this example
    .ToList()

使用新的API版本进行投影是否有更漂亮的方法?特别是我可以使用对列名的实际引用而不是字符串,如果稍后在实体中更改属性名称,这非常不方便且容易忘记。

1 个答案:

答案 0 :(得分:0)

不幸的是它看起来不像。 (我在寻找更漂亮的方式时找到了你的问题)

根据描述变化的teamblog条目;

  

注意,对于此版本,我们没有提供IQueryable   实现,所以正在将应用程序迁移到的开发人员   2.0发布并希望利用新表实现将需要使用提供的语法重建其查询。

Windows Azure Storage Client Library 2.0 Tables Deep Dive