AWS SDK for .NET Documentation
Query Method (queryRequest)
AmazonAmazon.DynamoDBv2AmazonDynamoDBClientQuery(QueryRequest) Did this page help you?   Yes   No    Tell us about it...

A Query operation directly accesses items from a table using the table primary key, or from an index using the index key. You must provide a specific hash key value. You can narrow the scope of the query by using comparison operators on the range key value, or on the index key. You can use the ScanIndexForward parameter to get results in forward or reverse order, by range key or by index key.

Queries that do not return results consume the minimum read capacity units according to the type of read.

If the total number of items meeting the query criteria exceeds the result set size limit of 1 MB, the query stops and results are returned to the user with a LastEvaluatedKey to continue the query in a subsequent operation. Unlike a Scan operation, a Query operation never returns an empty result set and a LastEvaluatedKey . The LastEvaluatedKey is only provided if the results exceed 1 MB, or if you have used Limit .

You can query a table, a local secondary index, or a global secondary index. For a query on a table or on a local secondary index, you can set ConsistentRead to true and obtain a strongly consistent result. Global secondary indexes support eventually consistent reads only, so do not specify ConsistentRead when querying a global secondary index.

Declaration Syntax
C#
public QueryResponse Query(
	QueryRequest queryRequest
)
Parameters
queryRequest (QueryRequest)
Container for the necessary parameters to execute the Query service method on AmazonDynamoDBv2.
Return Value
The response from the Query service method, as returned by AmazonDynamoDBv2.
Examples

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".

CopyQuery sample
// 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
{
    ComparisonOperator = "BEGINS_WITH",
    AttributeValueList = new List<AttributeValue>
    {
        new AttributeValue { S = "The Adventures" }
    }
};

// Create the key conditions from hashKey and condition
Dictionary<string, Condition> keyConditions = new Dictionary<string, Condition>
{
    // Hash key condition. ComparisonOperator must be "EQ".
    { 
        "Author",
        new Condition
        {
            ComparisonOperator = "EQ",
            AttributeValueList = new List<AttributeValue> { hashKey }
        }
    },
    // Range key condition
    {
        "Title",
        condition
    }
};

// Define marker variable
Dictionary<string, AttributeValue> startKey = null;

do
{
    // Create Query request
    QueryRequest request = new QueryRequest
    {
        TableName = "SampleTable",
        ExclusiveStartKey = startKey,
        KeyConditions = keyConditions
    };

    // 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 ?? new List<string>()),
                string.Join(", ", keyValuePair.Value.NS ?? new List<string>()));
        }
    }

    // Set marker variable
    startKey = result.LastEvaluatedKey;
} while (startKey != null);

The following example shows how to query items in a local secondary index.
Like a standard Query, we must pass in a hash-key value. But since we are querying against a specific index, we must remember to refer to the correct index attribute.
In the below example that means that we reference the attribute "Years", and since the attribute is numerical we must pass in a numerical AttributeValue.

CopyC#
// 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 ("Year", in "YearsIndex") that are less than 1900
Condition condition = new Condition
{
    ComparisonOperator = "LT",
    AttributeValueList = new List<AttributeValue>
    {
        new AttributeValue { N = "1900" }
    }
};

// Create the key conditions from hashKey and condition
Dictionary<string, Condition> keyConditions = new Dictionary<string, Condition>
{
    // Hash key condition. ComparisonOperator must be "EQ".
    { 
        "Author",
        new Condition
        {
            ComparisonOperator = "EQ",
            AttributeValueList = new List<AttributeValue> { hashKey }
        }
    },
    // Range key condition
    {
        "Year", // Reference the correct range key when using indexes
        condition
    }
};

// Define marker variable
Dictionary<string, AttributeValue> startKey = null;

do
{
    // Create Query request
    QueryRequest request = new QueryRequest
    {
        TableName = "SampleTable",
        ExclusiveStartKey = startKey,
        KeyConditions = keyConditions,
        IndexName = "YearsIndex" // Specify the index to query against
    };

    // 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 ?? new List<string>()),
                string.Join(", ", keyValuePair.Value.NS ?? new List<string>()));
        }
    }

    // Set marker variable
    startKey = result.LastEvaluatedKey;
} while (startKey != null);
Exceptions

Assembly: AWSSDK (Module: AWSSDK) Version: 1.5.60.0 (1.5.60.0)