Gets the values of one or more items and its attributes by primary key (composite primary key, only).
Narrow the scope of the query using comparison operators on the RangeKeyValue of the composite key. Use the ScanIndexForward parameter to get results in forward or reverse order by range key.
C# |
public QueryResponse Query( QueryRequest queryRequest )
- queryRequest (QueryRequest)
- Container for the necessary parameters to execute the Query service method on AmazonDynamoDB.
The following example shows how to query items in a table.
Note: the Query operation retrieves items that have the same
hash-key. This means that the Query operation is only supported on tables
with both a hash- and a range-key.
Note: the RangeKeyCondition for Query is limited to indexable comparisons.
These are EQ, LE, LT, GE, GT, BETWEEN, and BEGINS_WITH.
We will now retrieve all items where the hash-key is "Mark Twain" and
the range-key begins with the string "The Adventures".
// Create a client AmazonDynamoDBClient client = new AmazonDynamoDBClient(); // Define item hash-key to be string value "Mark Twain" AttributeValue hashKey = new AttributeValue { S = "Mark Twain" }; // Define query condition to search for range-keys that begin with the string "The Adventures" Condition condition = new Condition(); condition.WithComparisonOperator("BEGINS_WITH"); condition.WithAttributeValueList(new AttributeValue { S = "The Adventures" }); // Define marker variable Key startKey = null; do { // Create Query request QueryRequest request = new QueryRequest { TableName = "SampleTable", ExclusiveStartKey = startKey, HashKeyValue = hashKey, RangeKeyCondition = condition }; // Issue request QueryResult result = client.Query(request).QueryResult; // View all returned items List<Dictionary<string, AttributeValue>> items = result.Items; foreach (Dictionary<string, AttributeValue> item in items) { Console.WriteLine("Item:"); foreach (var keyValuePair in item) { Console.WriteLine("{0} : S={1}, N={2}, SS=[{3}], NS=[{4}]", keyValuePair.Key, keyValuePair.Value.S, keyValuePair.Value.N, string.Join(", ", keyValuePair.Value.SS.ToArray()), string.Join(", ", keyValuePair.Value.SS.ToArray())); } } // Set marker variable startKey = result.LastEvaluatedKey; } while (startKey != null);