简体   繁体   中英

Exception when filtering on DateTime field in Azure Table Storage

I'm having a kind of bizzare issue while trying to filter results from Azure Table Storage by a DateTime field. I have two different queries for two diffferent tables with two different models. Both need to filter on a DateTime field but only one works, the other retuns an exception:

Microsoft.WindowsAzure.Storage.StorageException was unhandled by user code
HResult=-2146233088
Message=The remote server returned an error: (400) Bad Request.
Source=Microsoft.WindowsAzure.Storage
StackTrace:
    at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T](RESTCommand`1 cmd, IRetryPolicy policy, OperationContext operationContext)

The queries:

 var fromDate = DateTime.UtcNow.AddDays(-14);

Working one:

 from entity in AnalyticsStorage.ConversionTable.CreateQuery<ConversionAnalyticsModel>()
 where entity.Date > fromDate
 select entity

Not Working:

 from entity in AnalyticsStorage.Table.CreateQuery<AnalyticsTableModel>()
 where entity.ResponseTime > fromDate
 select entity

I've made sure the the DateTime field is actually populated in the table. Commenting out the where makes the query run, though obviously that's not very useful.

Has anyone run into this? Are there gotachas in Azure Table Storage that I could be running into but can't see?

This is built against the current version of the Azure Storage DLL (3.0.3) and both queries are side by side in the same class, just in different methods.

I can't say I know why this is happening but after some time with Fiddler I figured out that it was using an anonymous type for the field name:

() gt datetime'2014-02-27T21:55:16.9605195Z' 

Should be:

RequestTime gt datetime'2014-02-27T21:55:16.9605195Z'

Once I noticed this I wrote a query using the pre-LINQ syntax:

var query = new TableQuery<AnalyticsTableModel>().Where(
                TableQuery.GenerateFilterConditionForDate(
                    "ResponseTime", 
                     QueryComparisons.GreaterThan,  
                     fromDate));

And this worked perfectly. So there seems to be a consistency issue with the current LINQ library that is causing it to not always map the field in the model to the field in the table. It could be that both RequestTime and ResponseTime are protected words to so my using their names is breaking the implementation without it properly warning me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM