3

我注意到,当我在 CloudTableQuery 上调用 Execute() 时,它不会立即向 Azure 发出请求。以这段代码为例:

var results = 
(from e in tableContext.CreateQuery<T>(tableName)
where e.PartitionKey == something
select e).AsTableServiceQuery().Execute();

仅当我稍后使用结果时才向 Azure 发出请求:

foreach(var item in results) ...

如果是这种情况,那么在我的代码片段中显式调用 Execute 是没有意义的。我对这种行为感到有些困惑,这让我问:在哪些情况下需要公开公开 Execute()?

4

3 回答 3

0

在编写了更多查询之后,似乎这Execute()对 LINQ 查询组合很有帮助。表客户端不提供对某些 LINQ 运算符的支持,例如Any()Execute()可以使用方法在IEnumerable<T>.

(from e in tableContext.CreateQuery<T>(tableName)
 where e.PartitionKey == "something"
 select e).AsTableServiceQuery(tableContext).Execute().Any()
于 2012-11-15T19:29:28.697 回答
0

如果您查看GitHub 上TableServiceQuery.cs的源代码,您将看到以下内容。深入挖掘,您可以看到重试策略、服务器超时和最大执行时间等都是通过调用 Execute() 设置的

    [DoesServiceRequest]
    public IEnumerable<TElement> Execute(TableRequestOptions requestOptions = null, OperationContext operationContext = null)
    {
        requestOptions = TableRequestOptions.ApplyDefaults(requestOptions, this.Context.ServiceClient);
        operationContext = operationContext ?? new OperationContext();
        long takeCount = TableUtilities.GetQueryTakeCount(this.Query, long.MaxValue);
        return
            General.LazyEnumerable(
                (continuationToken) =>
                this.ExecuteSegmentedCore((TableContinuationToken)continuationToken, requestOptions, operationContext),
                takeCount,
                operationContext);
    }
于 2012-11-01T19:14:46.817 回答
0

Linq 查询在您开始使用时执行,因此对于IEnumerable,您的查询在您开始枚举时执行。

var query = (from e in tableContext.CreateQuery<T>(tableName)
    where e.PartitionKey == "something"
    select e).AsTableServiceQuery(tableContext).Execute();


foreach(var element in query)
{ 
    // the query has been executed
}

因此,您需要转换查询以确保执行(.First(), .ToList()):

// Retreive the fisrt element
var element = (from e in tableContext.CreateQuery<T>(tableName)
    where e.PartitionKey == "something"
    select e).AsTableServiceQuery(tableContext).Execute().First();

// Retreive all the elements
var elements = (from e in tableContext.CreateQuery<T>(tableName)
    where e.PartitionKey == "something"
    select e).AsTableServiceQuery(tableContext).Execute().ToList();
于 2015-12-14T23:10:03.303 回答