Queries in Azure Tables

http://convective.wordpress.com/2010/02/06/queries-in-azure-tables/

This is a follow-up to a post on Azure Tables providing additional information on queries against the Azure Table Service.

CreateQuery<T>()

There are several classes involved in querying Azure Tables using the Azure Storage Client library. However, there is a single method central to the querying process and that isCreateQuery<T>() in the DataServiceContext class. CreateQuery<T>() is declared:

public DataServiceQuery<T> CreateQuery<T>(String entitySetName);

This method is used implicitly or explicitly in every query against Azure Tables using the Storage Client library.The CreateQuery<T>() return type is DataServiceQuery which implements both the IQueryable<T> and IEnumerable<T> interfaces. IQueryable<T>provides functionality to query data sources while IEnumerable<T> provides functionality to enumerate the results of these queries.

The following example demonstrates a trivial use of CreateQuery<T>() and the Take() operator to retrieve ten records from the Songs table in Development Storage:

public Type ResolveEntityType(String name)
{
    return typeof(Song);
}

protected void SimpleQuery(CloudTableClient cloudTableClient)
{
    TableServiceContext tableServiceContext = cloudTableClient.GetDataServiceContext();
    tableServiceContext.ResolveType = ResolveEntityType;
    IQueryable<Song> songs = (from entity in tableServiceContext.CreateQuery<Song>(“Songs”) select entity).Take(10);
    List<Song> songsList = songs.ToList<Song>();
}

As with other LINQ implementations thequery is not submitted to the Storage Service until the query results are enumerated by ToList<Song>().

CloudTableQuery

The DataServiceQuery class does not provide any methods to support continuation of queries when the Azure Table Service returns continuation tokens indicating that there are additional query results awaiting retrieval. The CloudTableQuery<T> class provides that support. 

If you’re using the REST API to access the table, the HTTP response header returns a continuation token. If you resubmit the same query with this continuation token in the header of the request, it will resume the query where it left off, returning the next page.

When using the SDK API, the presence of continuation tokens is not obvious, because it’s not explicitly exposed by the API (although there is a way to access them). The good news is that the SDK API knows how to handle the continuation so you don’t need to, by letting a CloudTableQuery object execute the query for you. 

AsTableServiceQuery() is an extension function that returns a new CloudTableQuery object that wraps the original query object with the code to handle continuations.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值