Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
1 task
bilalsaeedvisnext opened this issue Dec 29, 2024 · 1 comment
Assignees
Labels
bug This issue is a bug. dynamodb p3 This is a minor priority issue response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days.

Comments

@bilalsaeedvisnext
Copy link

bilalsaeedvisnext commented Dec 29, 2024

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

@bilalsaeedvisnext bilalsaeedvisnext added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Dec 29, 2024
@ashishdhingra ashishdhingra self-assigned this Dec 30, 2024
@ashishdhingra ashishdhingra added investigating This issue is being investigated and/or work is in progress to resolve the issue. p3 This is a minor priority issue and removed needs-triage This issue or PR still needs to be triaged. labels Dec 30, 2024
@ashishdhingra
Copy link
Contributor

@bilalsaeedvisnext Good morning. Thanks for opening the issue. This should ideally be opened as a Q&A discussion instead of a bug. It appears that you are using the API incorrectly.

If you refer API reference for DynamoDBContext.QueryAsync(), it expects first attribute to be HashKey value. This is true for any overload of Query/QueryAsync per API reference for DynamoDBContext. Instead you are trying to use QueryRequest model class from Amazon.DynamoDBv2.Model namespace. These model classes are normally used with AmazonDynamoDBClient class directly.

Below are some reference pages for examples:

Thanks,
Ashish

@ashishdhingra ashishdhingra added response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. and removed investigating This issue is being investigated and/or work is in progress to resolve the issue. labels Dec 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. dynamodb p3 This is a minor priority issue response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days.
Projects
None yet
Development

No branches or pull requests

2 participants