

La AWS SDK per .NET V3 è entrata in modalità manutenzione.

[Ti consigliamo di migrare alla V4.AWS SDK per .NET](https://docs.aws.amazon.com/sdk-for-net/v4/developer-guide/welcome.html) Per ulteriori dettagli e informazioni su come eseguire la migrazione, consulta il nostro annuncio sulla modalità di [manutenzione](https://aws.amazon.com/blogs/developer/aws-sdk-for-net-v3-maintenance-mode-announcement/).

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

# Utilizzo di espressioni con Amazon DynamoDB e AWS SDK per .NET
<a name="dynamodb-expressions"></a>

**Nota**  
Le informazioni contenute in questo argomento si riferiscono specificamente ai progetti basati su.NET Framework e alla AWS SDK per .NET versione 3.3 e precedenti.

I seguenti esempi di codice mostrano come utilizzare DynamoDB AWS SDK per .NET per programmare DynamoDB con espressioni. *Le espressioni* indicano gli attributi che si desidera leggere da un elemento in una tabella DynamoDB. Inoltre, puoi usare le espressioni durante la scrittura di un item per indicare qualsiasi condizione che deve essere soddisfatta (operazione detta anche *aggiornamento condizionale*) e per indicare le modalità di aggiornamento degli attributi. Alcuni esempi di aggiornamento sostituiscono l'attributo con un nuovo valore o aggiungono nuovi dati a una lista o una mappa. Per ulteriori informazioni, consulta [Lettura e scrittura di item utilizzando le espressioni](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.html).

**Topics**
+ [Dati di esempio.](#dynamodb-expressions-sample-data)
+ [Ottenere un singolo item utilizzando le espressioni e la chiave primaria dell'item](#dynamodb-expressions-get-item)
+ [Ottenere più item utilizzando le espressioni e la chiave primaria della tabella](#dynamodb-expressions-query)
+ [Ottenere più item utilizzando le espressioni e altri attributi degli item](#dynamodb-expressions-scan)
+ [Stampare un item](#dynamodb-expressions-print-item)
+ [Creare o sostituire un item utilizzando le espressioni](#dynamodb-expressions-put-item)
+ [Aggiornare un item utilizzando le espressioni](#dynamodb-expressions-update-item)
+ [Eliminare un item utilizzando le espressioni](#dynamodb-expressions-delete-item)
+ [Ulteriori informazioni](#dynamodb-expressions-resources)

## Dati di esempio.
<a name="dynamodb-expressions-sample-data"></a>

Gli esempi di codice in questo argomento si basano sui seguenti due elementi di esempio in una tabella DynamoDB denominata. `ProductCatalog` Questi elementi descrivono le informazioni sulle voci relative ai prodotti in un catalogo di biciclette fittizio. Questi elementi si basano sull'esempio fornito in [Case Study: A ProductCatalog ](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.CaseStudy.html) Item. I descrittori del tipo di dati come `BOOL`, `L`, `M`, `N`, `NS`, `S` e `SS` corrispondono a quelli nel [Formato di dati JSON](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DataFormat.html).

```
{
  "Id": {
    "N": "205"
  },
  "Title": {
    "S": "20-Bicycle 205"
  },
  "Description": {
    "S": "205 description"
  },
  "BicycleType": {
    "S": "Hybrid"
  },
  "Brand": {
    "S": "Brand-Company C"
  },
  "Price": {
    "N": "500"
  },
  "Gender": {
    "S": "B"
  },
  "Color": {
    "SS": [
      "Red",
      "Black"
    ]
  },
  "ProductCategory": {
    "S": "Bike"
  },
  "InStock": {
    "BOOL": true
  },
  "QuantityOnHand": {
    "N": "1"
  },
  "RelatedItems": {
    "NS": [
      "341",
      "472",
      "649"
    ]
  },
  "Pictures": {
    "L": [
      {
        "M": {
          "FrontView": {
            "S": "http://example/products/205_front.jpg"
          }
        }
      },
      {
        "M": {
          "RearView": {
            "S": "http://example/products/205_rear.jpg"
          }
        }
      },
      {
        "M": {
          "SideView": {
            "S": "http://example/products/205_left_side.jpg"
          }
        }
      }
    ]
  },
  "ProductReviews": {
    "M": {
      "FiveStar": {
        "SS": [
          "Excellent! Can't recommend it highly enough! Buy it!",
          "Do yourself a favor and buy this."
        ]
      },
      "OneStar": {
        "SS": [
          "Terrible product! Do not buy this."
        ]
      }
    }
  }
},
{
  "Id": {
    "N": "301"
  },
  "Title": {
    "S": "18-Bicycle 301"
  },
  "Description": {
    "S": "301 description"
  },
  "BicycleType": {
    "S": "Road"
  },
  "Brand": {
    "S": "Brand-Company C"
  },
  "Price": {
    "N": "185"
  },
  "Gender": {
    "S": "F"
  },
  "Color": {
    "SS": [
      "Blue",
      "Silver"
    ]
  },
  "ProductCategory": {
    "S": "Bike"
  },
  "InStock": {
    "BOOL": true
  },
  "QuantityOnHand": {
    "N": "3"
  },
  "RelatedItems": {
    "NS": [
      "801",
      "822",
      "979"
    ]
  },
  "Pictures": {
    "L": [
      {
        "M": {
          "FrontView": {
            "S": "http://example/products/301_front.jpg"
          }
        }
      },
      {
        "M": {
          "RearView": {
            "S": "http://example/products/301_rear.jpg"
          }
        }
      },
      {
        "M": {
          "SideView": {
            "S": "http://example/products/301_left_side.jpg"
          }
        }
      }
    ]
  },
  "ProductReviews": {
    "M": {
      "FiveStar": {
        "SS": [
          "My daughter really enjoyed this bike!"
        ]
      },
      "ThreeStar": {
        "SS": [
          "This bike was okay, but I would have preferred it in my color.",
	      "Fun to ride."
        ]
      }
    }
  }
}
```

## Ottenere un singolo item utilizzando le espressioni e la chiave primaria dell'item
<a name="dynamodb-expressions-get-item"></a>

L'esempio seguente illustra il metodo `Amazon.DynamoDBv2.AmazonDynamoDBClient.GetItem` e un set di espressioni per ottenere e quindi stampare l'item con un codice `Id` di `205`. Solo i seguenti attributi dell'item vengono restituiti: `Id`, `Title`, `Description`, `Color`, `RelatedItems`, `Pictures` e `ProductReviews`.

```
// using Amazon.DynamoDBv2;
// using Amazon.DynamoDBv2.Model;

var client = new AmazonDynamoDBClient();
var request = new GetItemRequest
{
  TableName = "ProductCatalog",
  ProjectionExpression = "Id, Title, Description, Color, #ri, Pictures, #pr",
  ExpressionAttributeNames = new Dictionary<string, string>
  {
    { "#pr", "ProductReviews" },
    { "#ri", "RelatedItems" }
  },
  Key = new Dictionary<string, AttributeValue>
  {
    { "Id", new AttributeValue { N = "205" } }
  },
};
var response = client.GetItem(request);

// PrintItem() is a custom function.
PrintItem(response.Item);
```

Nell'esempio precedente, la proprietà `ProjectionExpression` specifica gli attributi da restituire. La proprietà `ExpressionAttributeNames` specifica il segnaposto `#pr` per rappresentare l'attributo `ProductReviews` e il segnaposto `#ri` per rappresentare l'attributo `RelatedItems`. La chiamata a `PrintItem` si riferisce a una funzione personalizzata come descritto in [Stampare un item](#dynamodb-expressions-print-item).

## Ottenere più item utilizzando le espressioni e la chiave primaria della tabella
<a name="dynamodb-expressions-query"></a>

L'esempio seguente include il metodo `Amazon.DynamoDBv2.AmazonDynamoDBClient.Query` e un set di espressioni per ottenere e quindi stampare l'item con un codice `Id` di `301`, ma solo se il valore del codice `Price` è maggiore di `150`. Solo i seguenti attributi dell'item vengono restituiti: `Id`, `Title` e tutti gli attributi di `ThreeStar` 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("=====");
}
```

Nell'esempio precedente, la proprietà `ProjectionExpression` specifica gli attributi da restituire. La proprietà `ExpressionAttributeNames` specifica il segnaposto `#pr` per rappresentare l'attributo `ProductReviews` e il segnaposto `#p` per rappresentare l'attributo `Price`. `#pr.ThreeStar` specifica di restituire solo l'attributo `ThreeStar`. La proprietà `ExpressionAttributeValues` specifica il segnaposto `:val` per rappresentare il valore `150`. La proprietà `FilterExpression` specifica che `#p` (`Price`) deve essere maggiore di `:val` (`150`). La chiamata a `PrintItem` si riferisce a una funzione personalizzata come descritto in [Stampare un item](#dynamodb-expressions-print-item).

## Ottenere più item utilizzando le espressioni e altri attributi degli item
<a name="dynamodb-expressions-scan"></a>

L'esempio seguente include il metodo `Amazon.DynamoDBv2.AmazonDynamoDBClient.Scan` e un set di espressioni per ottenere e quindi stampare tutti gli item con un codice `ProductCategory` di `Bike`. Solo i seguenti attributi dell'item vengono restituiti: `Id`, `Title` e tutti gli attributi in `ProductReviews`.

```
// using Amazon.DynamoDBv2;
// using Amazon.DynamoDBv2.Model;

var client = new AmazonDynamoDBClient();
var request = new ScanRequest
{
  TableName = "ProductCatalog",
  ProjectionExpression = "Id, Title, #pr",
  ExpressionAttributeValues = new Dictionary<string,AttributeValue>
  {
    { ":catg", new AttributeValue { S = "Bike" } }
  },
  ExpressionAttributeNames = new Dictionary<string, string>
  {
    { "#pr", "ProductReviews" },
    { "#pc", "ProductCategory" }
  },
  FilterExpression = "#pc = :catg",  
};
var response = client.Scan(request);

foreach (var item in response.Items)
{
  // Write out the first page/scan of an item's attribute keys and values.
  // PrintItem() is a custom function.
  PrintItem(item);
  Console.WriteLine("=====");
}
```

Nell'esempio precedente, la proprietà `ProjectionExpression` specifica gli attributi da restituire. La proprietà `ExpressionAttributeNames` specifica il segnaposto `#pr` per rappresentare l'attributo `ProductReviews` e il segnaposto `#pc` per rappresentare l'attributo `ProductCategory`. La proprietà `ExpressionAttributeValues` specifica il segnaposto `:catg` per rappresentare il valore `Bike`. La proprietà `FilterExpression` specifica che `#pc` (`ProductCategory`) deve essere uguale a `:catg` (`Bike`). La chiamata a `PrintItem` si riferisce a una funzione personalizzata come descritto in [Stampare un item](#dynamodb-expressions-print-item).

## Stampare un item
<a name="dynamodb-expressions-print-item"></a>

L'esempio seguente mostra come stampare gli attributi e i valori di un item. Questo esempio viene utilizzato negli esempi precedenti che mostrano come [ottenere un singolo item utilizzando le espressioni e la chiave primaria dell'item](#dynamodb-expressions-get-item), [ottenere più item utilizzando le espressioni e la chiave primaria della tabella](#dynamodb-expressions-query) e [ottenere più item utilizzando le espressioni e altri attributi degli item](#dynamodb-expressions-scan).

```
// using Amazon.DynamoDBv2.Model;

// Writes out an item's attribute keys and values.
public static void PrintItem(Dictionary<string, AttributeValue> attrs)
{
  foreach (KeyValuePair<string, AttributeValue> kvp in attrs)
  {
    Console.Write(kvp.Key + " = ");
    PrintValue(kvp.Value);
  }
}

// Writes out just an attribute's value.
public static void PrintValue(AttributeValue value)
{
  // Binary attribute value.
  if (value.B != null)
  {
    Console.Write("Binary data");
  }
  // Binary set attribute value.
  else if (value.BS.Count > 0)
  {
    foreach (var bValue in value.BS)
    {
      Console.Write("\n  Binary data");
    }
  }
  // List attribute value.
  else if (value.L.Count > 0)
  {
    foreach (AttributeValue attr in value.L)
    {
      PrintValue(attr);
    }
  }
  // Map attribute value.
  else if (value.M.Count > 0)
  {
    Console.Write("\n");
    PrintItem(value.M);
  }
  // Number attribute value.
  else if (value.N != null)
  {
    Console.Write(value.N);
  }
  // Number set attribute value.
  else if (value.NS.Count > 0)
  {
    Console.Write("{0}", string.Join("\n", value.NS.ToArray()));
  }
  // Null attribute value.
  else if (value.NULL)
  {
    Console.Write("Null");
  }
  // String attribute value.
  else if (value.S != null)
  {
    Console.Write(value.S);
  }
  // String set attribute value.
  else if (value.SS.Count > 0)
  {
    Console.Write("{0}", string.Join("\n", value.SS.ToArray()));
  }
  // Otherwise, boolean value.
  else
  {
    Console.Write(value.BOOL);
  }
 
  Console.Write("\n");
}
```

Nell'esempio precedente, ogni valore di attributo ha diverse data-type-specific proprietà che possono essere valutate per determinare il formato corretto per stampare l'attributo. Queste proprietà includono `B`, `BOOL`, `BS`, `L`, `M`, `N`, `NS`, `NULL`, `S` e `SS`, che corrispondono a quelle nel [Formato di dati JSON](DataFormat.html). Per proprietà come `B`, `N`, `NULL` e `S`, se la proprietà corrispondente non è `null`, l'attributo è del corrispondente tipo di dati non-`null`. Per proprietà quali`BS`,,`L`, e `M` `NS``SS`, se `Count` è maggiore di zero, l'attributo è del tipo di non-zero-value dati corrispondente. Se tutte le data-type-specific proprietà dell'attributo sono `null` o sono `Count` uguali a zero, l'attributo corrisponde al tipo di `BOOL` dati.

## Creare o sostituire un item utilizzando le espressioni
<a name="dynamodb-expressions-put-item"></a>

L'esempio seguente include il metodo `Amazon.DynamoDBv2.AmazonDynamoDBClient.PutItem` e un set di espressioni per aggiornare l'item con un codice `Title` di `18-Bicycle 301`. Se l'item non esiste già, ne viene aggiunto uno nuovo.

```
// using Amazon.DynamoDBv2;
// using Amazon.DynamoDBv2.Model;

var client = new AmazonDynamoDBClient();
var request = new PutItemRequest
{
  TableName = "ProductCatalog",
  ExpressionAttributeNames = new Dictionary<string, string>
  {
    { "#title", "Title" }
  },
  ExpressionAttributeValues = new Dictionary<string, AttributeValue>
  {
    { ":product", new AttributeValue { S = "18-Bicycle 301" } }
  },
  ConditionExpression = "#title = :product", 
  // CreateItemData() is a custom function.
  Item = CreateItemData()
};
client.PutItem(request);
```

Nell'esempio precedente, la proprietà `ExpressionAttributeNames` specifica il segnaposto `#title` per rappresentare l'attributo `Title`. La proprietà `ExpressionAttributeValues` specifica il segnaposto `:product` per rappresentare il valore `18-Bicycle 301`. La proprietà `ConditionExpression` specifica che `#title` (`Title`) deve essere uguale a `:product` (`18-Bicycle 301`). La chiamata al codice `CreateItemData` si riferisce alla seguente funzione personalizzata:

```
// using Amazon.DynamoDBv2.Model;

// Provides a sample item that can be added to a table.
public static Dictionary<string, AttributeValue> CreateItemData()
{
  var itemData = new Dictionary<string, AttributeValue>
  {
    { "Id", new AttributeValue { N = "301" } },
    { "Title", new AttributeValue { S = "18\" Girl's Bike" } },
    { "BicycleType", new AttributeValue { S = "Road" } },
    { "Brand" , new AttributeValue { S = "Brand-Company C" } },
    { "Color", new AttributeValue { SS = new List<string>{ "Blue", "Silver" } } },
    { "Description", new AttributeValue { S = "301 description" } },
    { "Gender", new AttributeValue { S = "F" } },
    { "InStock", new AttributeValue { BOOL = true } },
    { "Pictures", new AttributeValue { L = new List<AttributeValue>{ 
      { new AttributeValue { M = new Dictionary<string,AttributeValue>{
        { "FrontView", new AttributeValue { S = "http://example/products/301_front.jpg" } } } } },
      { new AttributeValue { M = new Dictionary<string,AttributeValue>{
        { "RearView", new AttributeValue {S = "http://example/products/301_rear.jpg" } } } } },
      { new AttributeValue { M = new Dictionary<string,AttributeValue>{
        { "SideView", new AttributeValue { S = "http://example/products/301_left_side.jpg" } } } } }
    } } },
    { "Price", new AttributeValue { N = "185" } },
    { "ProductCategory", new AttributeValue { S = "Bike" } },
    { "ProductReviews", new AttributeValue { M = new Dictionary<string,AttributeValue>{
      { "FiveStar", new AttributeValue { SS = new List<string>{
        "My daughter really enjoyed this bike!" } } },
      { "OneStar", new AttributeValue { SS = new List<string>{
        "Fun to ride.",
        "This bike was okay, but I would have preferred it in my color." } } }
    } } },
    { "QuantityOnHand", new AttributeValue { N = "3" } },
    { "RelatedItems", new AttributeValue { NS = new List<string>{ "979", "822", "801" } } }
  };

  return itemData;
}
```

Nell'esempio precedente, viene restituito all'intermediario un item di esempio con dati campione. Una serie di attributi e valori corrispondenti vengono costruiti utilizzando tipi di dati come `BOOL`, `L`, `M`, `N`, `NS`, `S` e `SS`, che corrispondono a quelli nel [Formato di dati JSON](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DataFormat.html).

## Aggiornare un item utilizzando le espressioni
<a name="dynamodb-expressions-update-item"></a>

L'esempio seguente include il metodo `Amazon.DynamoDBv2.AmazonDynamoDBClient.UpdateItem` e un set di espressioni per modificare il codice `Title` in `18" Girl's Bike` per l'item con un codice `Id` di `301`.

```
// using Amazon.DynamoDBv2;
// using Amazon.DynamoDBv2.Model;

var client = new AmazonDynamoDBClient();
var request = new UpdateItemRequest
{
  TableName = "ProductCatalog",
  Key = new Dictionary<string,AttributeValue>
  {
     { "Id", new AttributeValue { N = "301" } }
  },
  ExpressionAttributeNames = new Dictionary<string, string>
  {
    { "#title", "Title" }
  },
  ExpressionAttributeValues = new Dictionary<string, AttributeValue>
  {
    { ":newproduct", new AttributeValue { S = "18\" Girl's Bike" } }
  },
  UpdateExpression = "SET #title = :newproduct"
};
client.UpdateItem(request);
```

Nell'esempio precedente, la proprietà `ExpressionAttributeNames` specifica il segnaposto `#title` per rappresentare l'attributo `Title`. La proprietà `ExpressionAttributeValues` specifica il segnaposto `:newproduct` per rappresentare il valore `18" Girl's Bike`. La proprietà `UpdateExpression` specifica di modificare il codice `#title` (`Title`) in `:newproduct` (`18" Girl's Bike`).

## Eliminare un item utilizzando le espressioni
<a name="dynamodb-expressions-delete-item"></a>

L'esempio seguente include il metodo `Amazon.DynamoDBv2.AmazonDynamoDBClient.DeleteItem` e un set di espressioni per eliminare il codice `Id` di `301` per l'item con un codice `Title` di `18-Bicycle 301`.

```
// using Amazon.DynamoDBv2;
// using Amazon.DynamoDBv2.Model;

var client = new AmazonDynamoDBClient();
var request = new DeleteItemRequest
{
  TableName = "ProductCatalog",
  Key = new Dictionary<string,AttributeValue>
  {
     { "Id", new AttributeValue { N = "301" } }
  },
  ExpressionAttributeNames = new Dictionary<string, string>
  {
    { "#title", "Title" }
  },
  ExpressionAttributeValues = new Dictionary<string, AttributeValue>
  {
    { ":product", new AttributeValue { S = "18-Bicycle 301" } }
  },
  ConditionExpression = "#title = :product"
};
client.DeleteItem(request);
```

Nell'esempio precedente, la proprietà `ExpressionAttributeNames` specifica il segnaposto `#title` per rappresentare l'attributo `Title`. La proprietà `ExpressionAttributeValues` specifica il segnaposto `:product` per rappresentare il valore `18-Bicycle 301`. La proprietà `ConditionExpression` specifica che `#title` (`Title`) deve essere uguale a `:product` (`18-Bicycle 301`).

## Ulteriori informazioni
<a name="dynamodb-expressions-resources"></a>

Per maggiori informazioni ed esempi di codice, consulta:
+  [DynamoDB Series - Expressions](http://blogs.aws.amazon.com/net/post/TxZQM7VA9AUZ9L/DynamoDB-Series-Expressions) 
+  [Accesso agli attributi degli item con le espressioni di proiezione](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.AccessingItemAttributes.html) 
+  [Utilizzo dei segnaposti per i nomi e i valori degli attributi](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ExpressionPlaceholders.html) 
+  [Specifica delle condizioni con le espressioni di condizione](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.SpecifyingConditions.html) 
+  [Modifica di item e attributi con le espressioni di aggiornamento](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.Modifying.html) 
+  [Lavorare con gli elementi utilizzando l'API di basso livello AWS SDK per .NET](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/LowLevelDotNetItemCRUD.html) 
+  [Interrogazione di tabelle utilizzando l'API di basso livello AWS SDK per .NET](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/LowLevelDotNetQuerying.html) 
+  [Scansione di tabelle utilizzando l'API di basso livello AWS SDK per .NET](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/LowLevelDotNetScanning.html) 
+  [Utilizzo degli indici secondari locali utilizzando l'API di basso livello AWS SDK per .NET](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/LSILowLevelDotNet.html) 
+  [Utilizzo degli indici secondari globali utilizzando l'API di basso livello AWS SDK per .NET](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSILowLevelDotNet.html) 