使用带有QueryOperator的DynamoDBContext.Query <t>和使用带有List <scancondition>的QueryFilter查询.NET中的DynamoDB

时间:2016-08-17 13:51:43

标签: c# datetime amazon-web-services amazon-dynamodb

我正在尝试使用带有HashKey和RangeKey的全局二级索引来查询DynamoDB表。

我想QUERY IndexCustomerIdCustomerIdDateTime范围(从 - 到)。没有选项将Index RangeKey设置为DateTime。唯一的选择是:StringNumberBinary

我在.NET Framework v3.5上使用 C#Object Persistence Model Amazon.DynamoDBv2.DataModel 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

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,但我使用的是更大的问题,我发现将参数值添加到对象数组中就修复了它。

所以可能是这样的:

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