User guide

Get Multiple Items by Using Expressions and the Table's
Primary Key
The following example features the Amazon.DynamoDBv2.AmazonDynamoDBClient.Query method
and a set of expressions to get and then print the item that has an Id of 301, but only if the value of
Price is greater than 150. Only the following attributes of the item are returned: Id, Title, and all of
the ThreeStar attributes in ProductReviews.
// using Amazon.DynamoDBv2;
// using Amazon.DynamoDBv2.Model;
var client = new AmazonDynamoDBClient();
var request = new QueryRequest
{
TableName = "ProductCatalog",
KeyConditions = new Dictionary<string,Condition>
{
{ "Id", new Condition()
{
ComparisonOperator = ComparisonOperator.EQ,
AttributeValueList = new List<AttributeValue>
{
new AttributeValue { N = "301" }
}
}
}
},
ProjectionExpression = "Id, Title, #pr.ThreeStar",
ExpressionAttributeNames = new Dictionary<string, string>
{
{ "#pr", "ProductReviews" },
{ "#p", "Price" }
},
ExpressionAttributeValues = new Dictionary<string,AttributeValue>
{
{ ":val", new AttributeValue { N = "150" } }
},
FilterExpression = "#p > :val"
};
var response = client.Query(request);
foreach (var item in response.Items)
{
// Write out the first page of an item's attribute keys and values.
// PrintItem() is a custom function.
PrintItem(item);
Console.WriteLine("=====");
}
In the preceding example, the ProjectionExpression property specifies the attributes to be returned.
The ExpressionAttributeNames property specifies the placeholder #pr to represent the
ProductReviews attribute and the placeholder #p to represent the Price attribute. #pr.ThreeStar
specifies to return only the ThreeStar attribute.The ExpressionAttributeValues property specifies
the placeholder :val to represent the value 150.The FilterExpression property specifies that #p
(Price) must be greater than :val (150).The call to PrintItem refers to a custom function as described
in Print an Item (p. 58).
Version v2.0.0
57
AWS SDK for .NET Developer Guide
Using Expressions with DynamoDB