- Newest
- Most votes
- Most comments
QueryFilter should be a List<ScanCondition> like this:
await ddbContext.QueryAsync<User>(customerName, new DynamoDBOperationConfig
{
IndexName = "customer-users",
QueryFilter = new List<ScanCondition>
{
new ScanCondition(nameof(User.IsActive), ScanOperator.Equal, true)
}
})
.GetRemainingAsync();
To query a Global Secondary Index (GSI) with an additional filter using DynamoDBContext while taking advantage of the DynamoDBGlobalSecondaryIndexHashKey attribute, you can modify your approach slightly. The DynamoDBContext in the AWS SDK for .NET provides a way to query GSIs while respecting the attributes you've declared on your model.
Here's how you can query the "customer-users" GSI for active users of a specific customer:
var queryConfig = new DynamoDBOperationConfig { IndexName = "customer-users", QueryFilter = new List<ScanCondition> { new ScanCondition("IsActive", ScanOperator.Equal, true) } }; var query = ddbContext.FromQueryAsync<User>(customerName, queryConfig); var activeUsers = await query.GetRemainingAsync();
In this approach:
-
We're using the
FromQueryAsync<User>method instead ofQueryAsync<User>. This method respects the DynamoDBGlobalSecondaryIndexHashKey attribute on the Customer property. -
We pass the
customerNameas the first parameter, which will be used as the hash key for the GSI query. -
We're still using the DynamoDBOperationConfig to specify the index name and the query filter.
-
The QueryFilter is applied using the property name "IsActive" as defined in your User class, not the DynamoDB attribute name. The DynamoDBContext will automatically translate this to the correct attribute name ("is_active") based on your DynamoDBProperty attribute.
This approach takes full advantage of the attributes you've declared on your User class. The DynamoDBGlobalSecondaryIndexHashKey attribute is respected for the query key, and the DynamoDBProperty attributes are used for translating between your C# property names and the actual DynamoDB attribute names.
Remember that when querying a GSI, you can only retrieve attributes that are projected into the index. Make sure that the IsActive attribute is projected if you want to filter on it.
Sources
Working with Global Secondary Indexes: .NET - Amazon DynamoDB
Working with Global Secondary Indexes: Java - Amazon DynamoDB
FromQueryAsyncdoesn't accept the GSI's hash key value as the first parameter. This code isn't valid.
Relevant content
- asked 8 years ago
- asked 4 years ago
- asked 3 years ago

Square bracket notation (collection expressions) is syntactic sugar for
new List<T>()as of C# 12. Your code and my code are equivalent.Oh thats nice, haven't used 12 yet. What exception message are you getting?
I get no exception at all. It's just not returning any data. If I remove
QueryFilteror switch to usingFromQueryAsyncand use the more verbose mode of adding the filter--i.e. usingFilterExpressionand building the string--I get results. It's just while usingQueryAsyncandQueryFilterthat I get no results instead of the ones I expect.