Amazon DynamoDB · Retrieve

Retrieve one record

Read one item by its key, in three scenarios: a product by its id, an order by its full key with its customer read back as a lookup, and an id no item has.

Set up for this page

The scenarios build on the sample application: SampleContext.cs (the dbContext every sample is given); SampleOutput.cs (Step and Show, which print); SampleContext.Application.cs (dbContext.Service, the application's ARX service); SampleModel.cs (the DynamoDB layout and the schema catalog); SampleContext.Direct.cs (dbContext.Direct); DynamoDirectHelper.cs (the checks behind dbContext.Direct).

dbContext.Direct is the samples' own checking code on the plain driver, not part of ARX: leave it out of your application.

Before scenario 1 the sample runs this, so the service and the rows the scenarios use are there:

Set up
var product = new ArxEntity("product");
product["sku"] = "SKU-D401";
product["name"] = "Hiking pole";
product["price"] = 34.50m;
await service.CreateAsync(product);
var order = new ArxEntity("sales_order");
order["customer_id"] = "C-D401";
order["order_number"] = "SO-D401";
order["status"] = "OPEN";
order["ordered_on"] = "2026-11-08";
await service.CreateAsync(order);

1 By id

A product by its SKU, the key and the id.

Already in scope: service (ArxDynamoDbService)

C#
ArxEntity? found = await service.RetrieveAsync("product", "SKU-D401");

Console.WriteLine($"{found!["name"]}, {found["price"]}");

Check it in the database

PartiQL

A PartiQL statement: run it in NoSQL Workbench, or with aws dynamodb execute-statement --statement, after the code has run.

SELECT "name", "price" FROM "product" WHERE "sku" = 'SKU-D401'

Expect: 1 item, Hiking pole, 34.5.

2 By the full key

An order by its customer and number; its customer_id comes back as a reference.

Already in scope: service (ArxDynamoDbService)

C#
var match = new ArxEntity("sales_order");
match["customer_id"] = "C-D401";
match["order_number"] = "SO-D401";

ArxEntity? found = await service.RetrieveAsync(match);
var customer = (ArxEntityReference)found!["customer_id"]!;

Console.WriteLine($"{found["order_number"]}: {found["status"]}, customer {customer.EntityName} {customer.RecordId!.AsString()}");

Check it in the database

PartiQL

A PartiQL statement: run it in NoSQL Workbench, or with aws dynamodb execute-statement --statement, after the code has run.

SELECT "order_number", "status", "customer_id" FROM "crm" WHERE "PK" = 'CUSTOMER#C-D401' AND "SK" = 'ORDER#SO-D401'

Expect: 1 item, SO-D401, OPEN, customer_id "C-D401".

3 An id no item has

A SKU no item has: null.

Already in scope: service (ArxDynamoDbService)

C#
ArxEntity? missing = await service.RetrieveAsync("product", "SKU-D499");

Console.WriteLine($"found: {missing != null}");

Check it in the database

PartiQL

A PartiQL statement: run it in NoSQL Workbench, or with aws dynamodb execute-statement --statement, after the code has run.

SELECT "sku" FROM "product" WHERE "sku" = 'SKU-D499'

Expect: no items.

Good to know

One GetItem, strongly consistent by default (it sees every write that succeeded before it). An order's key is its customer and its number, so give both. The catalog's lookup reads customer_id back as an ArxEntityReference. Not found is null.

Run the complete sample

The code above comes from one program, built in a fresh project and run against the database. To run it yourself, here is every file and step.

The program, its files and how to run it

Run it locally

  1. .NET SDK: .NET SDK 10.0.100 or later 10.0.x (target net10.0).
  2. Create the project:
    Terminal
    dotnet new console -n ArxSample -f net10.0
    cd ArxSample
  3. Install the packages:
    Terminal
    dotnet add package ARX.SDK.DynamoDb --version 1.0.0-rc.1
  4. Database: a Amazon DynamoDB, or DynamoDB Local for development (tested with DynamoDB Local 2.5.2) database with the sample schema.
  5. Configuration: set ARX_SAMPLES_DYNAMODB to its connection string. Nothing is hard-coded.
  6. Source code: add the files of the sample application this sample uses (SampleContext.cs, SampleOutput.cs, SampleContext.Application.cs, SampleModel.cs, SampleContext.Direct.cs, DynamoDirectHelper.cs), then replace Program.cs and add the other file below, with the file names shown.
  7. Run it:
    Terminal
    dotnet run
  8. Expected output: below.

The files

Program.cs

Runs the sample. Generated for this page; it reads the connection string from the environment and first remakes the sample database from sample-schema.json.

using ArxSamples.DynamoDb;
using ArxSamples.DynamoDb.Operations.Retrieve.Basics;

// The connection string comes from the environment, never from the code (see "Configure the connection").
var connectionString = Environment.GetEnvironmentVariable("ARX_SAMPLES_DYNAMODB")
    ?? throw new InvalidOperationException("Set the ARX_SAMPLES_DYNAMODB environment variable to your Amazon DynamoDB connection string.");

var dbContext = new SampleContext(connectionString);

// Every run starts from the sample data: this deletes and makes again what sample-schema.json lists
// (save the file in the project folder, see Get started).
await dbContext.Direct.ResetAsync();

await new Basic1_RetrieveOneRecord().RunAsync(dbContext);
Basic1_RetrieveOneRecord.cs

The sample, as it is in the repository.

using ARX.SDK.DynamoDb.Services;
using ARX.SDK.Services;

using static ArxSamples.DynamoDb.SampleOutput;

namespace ArxSamples.DynamoDb.Operations.Retrieve.Basics;

/// <summary>
/// Read one item by its key, in three scenarios: a product by its id, an order by its full key with its customer read
/// back as a lookup, and an id no item has.
/// </summary>
/// <remarks>
/// One <c>GetItem</c>, strongly consistent by default (it sees every write that succeeded before it). An order's key is
/// its customer and its number, so give both. The catalog's lookup reads <c>customer_id</c> back as an
/// <see cref="ArxEntityReference"/>. Not found is null.
/// </remarks>
public sealed class Basic1_RetrieveOneRecord : ISample
{
    public string Name => "retrieve-basics-1-retrieve-one-record";

    public string Title => "Retrieve one record";

    public async Task RunAsync(SampleContext dbContext)
    {
        var product = new ArxEntity("product");
        product["sku"] = "SKU-D401";
        product["name"] = "Hiking pole";
        product["price"] = 34.50m;
        await dbContext.Service.CreateAsync(product);

        var order = new ArxEntity("sales_order");
        order["customer_id"] = "C-D401";
        order["order_number"] = "SO-D401";
        order["status"] = "OPEN";
        order["ordered_on"] = "2026-11-08";
        await dbContext.Service.CreateAsync(order);

        await Scenario1_ByIdAsync(dbContext.Service);

        await Scenario2_ByTheFullKeyAsync(dbContext.Service);

        await Scenario3_AnIdNoItemHasAsync(dbContext.Service);
    }

    /// <summary>1. A product by its SKU, the key and the id.</summary>
    private static async Task Scenario1_ByIdAsync(ArxDynamoDbService service)
    {
        Step("1. By id");

        ArxEntity? found = await service.RetrieveAsync("product", "SKU-D401");

        Show($"{found!["name"]}, {found["price"]}");

        /* ===========================================================
         * [DIRECT QUERY CHECK]
         * -----------------------------------------------------------
         * SELECT "name", "price" FROM "product" WHERE "sku" = 'SKU-D401'
         * -- Expect: 1 item, Hiking pole, 34.5.
         * ===========================================================
         */
    }

    /// <summary>2. An order by its customer and number; its customer_id comes back as a reference.</summary>
    private static async Task Scenario2_ByTheFullKeyAsync(ArxDynamoDbService service)
    {
        Step("2. By the full key");

        var match = new ArxEntity("sales_order");
        match["customer_id"] = "C-D401";
        match["order_number"] = "SO-D401";

        ArxEntity? found = await service.RetrieveAsync(match);
        var customer = (ArxEntityReference)found!["customer_id"]!;

        Show($"{found["order_number"]}: {found["status"]}, customer {customer.EntityName} {customer.RecordId!.AsString()}");

        /* ===========================================================
         * [DIRECT QUERY CHECK]
         * -----------------------------------------------------------
         * SELECT "order_number", "status", "customer_id" FROM "crm" WHERE "PK" = 'CUSTOMER#C-D401' AND "SK" = 'ORDER#SO-D401'
         * -- Expect: 1 item, SO-D401, OPEN, customer_id "C-D401".
         * ===========================================================
         */
    }

    /// <summary>3. A SKU no item has: null.</summary>
    private static async Task Scenario3_AnIdNoItemHasAsync(ArxDynamoDbService service)
    {
        Step("3. An id no item has");

        ArxEntity? missing = await service.RetrieveAsync("product", "SKU-D499");

        Show($"found: {missing != null}");

        /* ===========================================================
         * [DIRECT QUERY CHECK]
         * -----------------------------------------------------------
         * SELECT "sku" FROM "product" WHERE "sku" = 'SKU-D499'
         * -- Expect: no items.
         * ===========================================================
         */
    }
}

Expected output

What dotnet run printed when the sample was verified, on a freshly created sample schema. Ids differ when the tables already hold rows.

Output
  > 1. By id
      Hiking pole, 34.5
  > 2. By the full key
      SO-D401: OPEN, customer customer C-D401
  > 3. An id no item has
      found: False