Skip to content

Unable to cast object of type 'Amazon.DynamoDBv2.Model.QueryRequest' to type 'System.String'. #3591

Closed
@bilalsaeedvisnext

Description

@bilalsaeedvisnext

Describe the bug

Unable to cast object of type Amazon.DynamoDBv2.Model.QueryRequest to type System.String.
I am querying Dynamo DB using .NET Core API. It was working with the Scan operation but I changed it to Query operation and also added a GSI where GroupID is the partition key and LogDateTime as sort key. I am first time using Dynamo DB so not sure what is the correct way to go with. I am getting this error in the code given below.

public async Task<List<ActivityLogEntity>> GetAllActivityLogs(string groupId, DateTime? fromDate, DateTime? toDate)
{
    var startDateTime = ExtensionHelper.GetDateTimeString(fromDate?.StartOfDay());
    var endDateTime = ExtensionHelper.GetDateTimeString(toDate?.EndOfDay());

    try
    {
        // Build the query request
        var queryRequest = new QueryRequest
        {
            TableName = "ActivityLogTable",
            IndexName = "GroupIDLogDateTimeIndex", // Use the GSI name
            KeyConditionExpression = "GroupID = :groupId",
            ExpressionAttributeValues = new Dictionary<string, AttributeValue>
            {
                { ":groupId", new AttributeValue { S = groupId } },
                //{ ":startDate", new AttributeValue { S = startDateTime } },
                //{ ":endDate", new AttributeValue { S = endDateTime } }
            }
        };

        // Execute the query
        var queryResults = await _context.QueryAsync<ActivityLogTable>(queryRequest).GetRemainingAsync();

        var response = new List<ActivityLogEntity>();

        if (queryResults.Any())
        {
            // Retrieve user details in one call if there are multiple UserIDs
            var userIds = queryResults.Select(x => x.UserID).Distinct().ToArray();
            //var userList = await _context.BatchGetAsync<UserTable>(userIds).GetRemainingAsync();

            foreach (var activityLog in queryResults)
            {
                //var user = userList.FirstOrDefault(x => x.UserID == activityLog.UserID);

                response.Add(new ActivityLogEntity
                {
                    UserID = activityLog.UserID,
                    LogID = activityLog.LogID,
                    EntityID = activityLog.EntityID,
                    //LogByName = activityLog.Activity == ActivityLogEnum.reminder_sent.ToString()
                    //    ? "System"
                    //    : $"{user?.FirstName ?? string.Empty} {user?.LastName ?? string.Empty}",
                    LogText = activityLog.LogText,
                    Remark = activityLog.Remark,
                    LogDateTime = activityLog.LogDateTime,
                    Activity = activityLog.Activity,
                    EntityType = activityLog.EntityType,
                    IsSystemLog = activityLog.IsSystemLog,
                    IsUserLog = activityLog.IsUserLog
                });
            }
        }

        return response.OrderByDescending(x => x.LogDateTime).ToList();
    }
    catch (Exception ex)
    {
        // Handle the exception (e.g., log it)
        Console.WriteLine($"An error occurred: {ex.Message}");
        return null;
    }
}

Regression Issue

  • Select this option if this issue appears to be a regression.

Expected Behavior

.NET Core API should successfully fetch data from Dynamo db using Query operation.

Current Behavior

Unable to cast object of type Amazon.DynamoDBv2.Model.QueryRequest to type System.String.

Reproduction Steps

On executing the above code

Possible Solution

No response

Additional Information/Context

No response

AWS .NET SDK and/or Package version used

Assembly AWSSDK.DynamoDBv2, Version=3.3.0.0

Targeted .NET Platform

.NET 6

Operating System and version

Windows 10

Metadata

Metadata

Assignees

Labels

bugThis issue is a bug.closed-for-stalenessdynamodbp3This is a minor priority issueresponse-requestedWaiting on additional info and feedback. Will move to "closing-soon" in 7 days.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions