1

I want to query a GSI composite key using OPM. I followed the documentation example the link for which is as follows: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBContext.QueryScan.html

However I am facing two issues:

1). The same code as mentioned in the above example is throwing errors for me. I believe inside the context.QueryAsync(Hask Key target value, Operator.Between, RangeKey lower target value, RangeKey higher target value) is the syntax.

But I get error as follows for the last two range key target values: Error CS1503 Argument 3: cannot convert from 'string' to 'System.Collections.Generic.IEnumerable'

2). How do I set the query to target a GSI instead of Normal Composite Keys.

For instructing the query to target GSI I googled and found the below code snippet : DynamoDBOperationConfig(). But how do I incorporate it into the final Context.QueryAsync method?

using Amazon.DynamoDBv2.DataModel;
using System;
using System.Collections.Generic;
using System.Text;

namespace EDCPA.Core.Models
{
    [DynamoDBTable("EDCBUILDDATA1")]
    public class AARes
    {

        [DynamoDBGlobalSecondaryIndexHashKey("PN-FGD-Index")]
        public string ProjectName { get; set; }

        [DynamoDBGlobalSecondaryIndexRangeKey("PN-FGD-Index")]
        public string FileGeneratedDate { get; set; }

        public string VehicleName { get; set; }

        public string FileNameKey { get; set; }

        [DynamoDBHashKey]
        public string Id { get; set; }

        [DynamoDBRangeKey]
        public string createdTimeStamp { get; set; }


    }
}
using Amazon.DynamoDBv2.DataModel;
using System;
using System.Collections.Generic;
using System.Text;

namespace EDCPA.Core.Models
{
    [DynamoDBTable("EDCBUILDDATA1")]
    public class AAReq
    {
        public string FileGeneratedFromDate { get; set; }

        public string FileGeneratedToDate { get; set; }

        public string ProjectName { get; set; }

        public string VehicleName { get; set; }

        [DynamoDBHashKey]
        public string Id { get; set; }

        [DynamoDBRangeKey]
        public string createdTimeStamp { get; set; }
    }
}

public async Task<IActionResult> AARequestAsync([FromBody] AAReq req)
        {
            try
            {
                headers = HeaderCollections.TryRetrieveToken(Request);


AmazonDynamoDBClient client = new AmazonDynamoDBClient(new StoredProfileAWSCredentials(),
                     RegionEndpoint.APSouth1);

                DynamoDBContext context = new DynamoDBContext(client);

                DynamoDBOperationConfig indexHashRangeOpConfig = new DynamoDBOperationConfig()
                {
                    IndexName = "PN-FGD-Index",
                    ConsistentRead = false,
                };

                IEnumerable<AARes> fileKeys = await
                context.QueryAsync<AARes>(req.ProjectName, QueryOperator.Between, req.FileGeneratedFromDate, req.FileGeneratedToDate);
                Console.WriteLine("\nFindRepliesInLast15Days: Printing result.....");
                foreach (AARes r in fileKeys)
                    Console.WriteLine("{0}\t{1}\t{2}\t{3}", r.FileNameKey, r.VehicleName);

1). What am I doing wrong inside the context.QueryAsync statement?

2). Also,How do I incorporate the DynamoDBOperationConfig indexHashRangeOpConfig into my final query statement?

Gowthaman
  • 59
  • 9

1 Answers1

1

Here is the solution to my problems above:

List<Dashboardreq> list =
                await context.QueryAsync<Dashboardreq>(req.ProjectName, QueryOperator.Between, new string[] {
                                                 req.FileGeneratedFromDate+" " + Constants.DayBeginTime,
                                                 req.FileGeneratedToDate+" " + Constants.DayEndTime
                                                 }, indexHashRangeOpConfig).GetRemainingAsync();
Gowthaman
  • 59
  • 9