2016-08-17 49 views
1

我試圖使用具有HashKey和RangeKey的全局二級索引來查詢DynamoDB表。使用DynamoDBContext查詢.NET中的DynamoDB查詢<T> with QueryOperator vs QueryFilter with List <ScanCondition>

我想要QUERYIndexCustomerIdCustomerIdDateTime範圍(從 - 到)。沒有選項將Index RangeKey設置爲DateTime。唯一的選擇是:String,NumberBinary

我正在使用C#對象持久性模型Amazon.DynamoDBv2.DataModel .NET Framework v3.5上的API。

表:

+-------------------+----------+---------------------------+----------------+------------+ 
| InputFlowCode  | Counter | OrderIssueDate   | OrderTypeCode | CustomerId | 
+-------------------+----------+---------------------------+----------------+------------+ 
| bac9-35df6ac533fc | 000004 | 2016-07-19T22:00:00.000Z | 220   | 123  | 
+-------------------+----------+---------------------------+----------------+------------+ 
| a3db-9d6f56a5c611 | 000006 | 2016-06-30T22:00:00.000Z | 220   | 456  | 
+-------------------+----------+---------------------------+----------------+------------+ 
| af1c-db5b089c1e32 | 000010 | 2016-07-02T22:00:00.000Z | 220   | 789  | 
+-------------------+----------+---------------------------+----------------+------------+ 
| ...    | ...  | ...      | ...   | ...  | 
+-------------------+----------+---------------------------+----------------+------------+ 

全球二級索引定義:

IndexCustomerId: 
- CustomerId (integer), 
- OrderIssueDate (DateTime on model, but string on Index definition) 

代碼:

try 
{ 
    DateTime dateFrom = new DateTime(2016, 08, 01); 
    DateTime dateTo = new DateTime(2016, 08, 15); 

    List<MyDynamoDBItem> items = new List<MyDynamoDBItem>(); 

    DynamoDBOperationConfig operationConfig = new DynamoDBOperationConfig(); 
    operationConfig.OverrideTableName = "Transactions"; 
    operationConfig.IndexName = "IndexCustomerId"; 

    DynamoDBContext context = new DynamoDBContext(DynamoDBClient); 

    // 1) Works 
    items = context.Query<MyDynamoDBItem>(customerId, operationConfig).OrderByDescending(o => o.OrderIssueDate).ToList(); 

    // 2) Doesn't work 
    items = context.Query<MyDynamoDBItem>(customerId, QueryOperator.Between, dateFrom, dateTo, operationConfig).OrderByDescending(o => o.OrderIssueDate).ToList(); 

    // 3) Works, but I don't know if it is the right choice... 
    List<ScanCondition> conditions = new List<ScanCondition>(); 
    conditions.Add(new ScanCondition(PeppolOrdineDynamoDBTableAttributes.OrderIssueDate, ScanOperator.Between, dateFrom, dateTo)); 
    operationConfig.QueryFilter = conditions; 
    items = context.Query<MyDynamoDBItem>(customerId, operationConfig).OrderByDescending(o => o.OrderIssueDate).ToList(); 
} 
catch (Exception) 
{ 
    throw; 
} 

ŧ他第一個查詢沒有參數的作品。

第二查詢Between操作員和2 DateTime拋出Exception

{"Cannot cast objects of type 'System.Int32' to type 'System.String'."} 

第三個查詢使用QueryFilter是一個列表和ScanOperator工作過,但我不知道知道QueryOperator有什麼區別,如果這是正確的選擇,因爲我想製作一個QUERY而不是SCAN

回答

0

我有同樣的問題,但我使用greaterThan,我發現將參數值添加到對象數組固定它。

因此,也許是這樣的:

items = context.Query<MyDynamoDBItem>(customerId, QueryOperator.Between, new object[] {dateFrom, dateTo}, operationConfig).OrderByDescending(o => o.OrderIssueDate).ToList();