Skip to main content

Scan Request Builder

Overview

This section describes the EfficientDynamoDb API for building the Scan request.

The IScanEntityRequestBuilder<TEntity> interface provides a builder pattern for constructing a Scan operation in DynamoDB. It is designed to work with a database entity of type TEntity.

Use the DynamoDbContext.Scan<TEntity>() method to get the builder:

var builder = ddbContext.Scan<EntityClass>();

There are no required configuration methods for the Scan request. All builder methods are optional and can be omitted. In this case, DynamoDB will use the default behavior.

Scan Configuration

FromIndex

Specifies the index name to use for the Scan operation. Can be used for both GSI and LSI.

IScanEntityRequestBuilder<TEntity> FromIndex(string indexName);

Parameters

  • indexName: Name of the index.

Example

builder = builder.FromIndex("indexName");

WithConsistentRead

Specifies whether to use a consistent read in the Scan operation.

IScanEntityRequestBuilder<TEntity> WithConsistentRead(bool useConsistentRead);

If not specified, DynamoDB will use eventually consistent read.

caution

Consistent reads are not supported for Scan requests against GSIs.

Parameters

  • useConsistentRead: Set this to true if you want a consistent read. Otherwise, set it to false. Setting it to false is equivalent to not using the WithConsistentRead() method at all.

Example

builder = builder.WithConsistentRead(true);

WithLimit

Specifies the maximum number of items to query.

IScanEntityRequestBuilder<TEntity> WithLimit(int limit);
info

The actual number of items returned may be less than specified when filter expression is present or if the scan operation exceeds the 1 MB limit or retrieved data. Refer to AWS developer guide for more information.

Parameters

  • limit: Maximum number of items to query.

Example

builder = builder.WithLimit(50);

ReturnConsumedCapacity

Specifies the consumed capacity details to include in the response.

IScanEntityRequestBuilder<TEntity> ReturnConsumedCapacity(ReturnConsumedCapacity consumedCapacityMode);

If not specified, no consumed capacity info is returned in the response.

Parameters

  • consumedCapacityMode: The type of consumed capacity information to return. Setting it to ReturnConsumedCapacity.None is equivalent to not using the ReturnConsumedCapacity() method at all.

Example

builder = builder.ReturnConsumedCapacity(ReturnConsumedCapacity.Total);

WithSelectMode

Specify the select mode for the Scan operation. It affects what data will be returned in response.

IScanEntityRequestBuilder<TEntity> WithSelectMode(Select selectMode);

Parameters

  • selectMode: Select mode to use for the query operation. Learn more about possible modes here.
tip

Use one of the projection methods instead of specifying SpecificAttributes mode:

Example

builder = builder.WithSelectMode(Select.Count);

BackwardSearch

Specifies if backward search should be used.

IScanEntityRequestBuilder<TEntity> BackwardSearch(bool useBackwardSearch);

Parameters

  • useBackwardSearch: true, if backward search should be used. Otherwise, false.

Example

builder = builder.BackwardSearch(true);

WithFilterExpression (Explicit condition)

Specifies the filter expression for the Scan operation.

IScanEntityRequestBuilder<TEntity> WithFilterExpression(FilterBase filterExpressionBuilder);

Parameters

Example

var expr = Condition<EntityClass>.On(item => item.FirstName).EqualTo("John");
builder = builder.WithFilterExpression(expr);

WithFilterExpression (Function condition)

Specifies the filter expression function for the Scan operation.

IScanEntityRequestBuilder<TEntity> WithFilterExpression(Func<EntityFilter<TEntity>, FilterBase> filterSetup);

Parameters

Example

builder = builder.WithFilterExpression(
cond => cond.On(item => item.FirstName).EqualTo("John")
);

WithPaginationToken

Specifies the pagination token for the Scan operation.

IScanEntityRequestBuilder<TEntity> WithPaginationToken(string? paginationToken);

Parameters

  • paginationToken: The pagination token to use. Passing null results in the same behavior as not specifying the pagination token at all.

Example

builder = builder.WithPaginationToken("yourToken");

AsProjections (With type)

Projects the retrieved items to the specified type. Only properties present in TProjection will be retrieved.

This method returns a different type of the builder to preserve the projection type. In case of chained calls and/or using var to save builder to a variable, the change of returned type may be unnoticeable. This is by design and you should be able to mix regular and projected builders.

IScanEntityRequestBuilder<TEntity, TProjection> AsProjections<TProjection>() where TProjection : class;

Example

Since the change of returned builder type, it can't be assigned to the same variable.

var projectedBuilder = builder.AsProjections<ProjectedEntity>();

AsProjections (With attributes)

Projects the retrieved items to the specified type, but only retrieves the properties specified in properties parameter. Other properties will have default values.

Similarly to AsProjection<TProjection>(), this method returns a different type of the builder to preserve the projection type with all the previously explained consequences.

IScanEntityRequestBuilder<TEntity, TProjection> AsProjections<TProjection>(params Expression<Func<TProjection, object>>[] properties) where TProjection : class;

Parameters

  • properties: The attributes to project.

Example

var projectedBuilder = builder.AsProjections<ProjectedEntity>(
x => x.SomeProperty,
x => x.AnotherProperty
);

After execution, this Scan request will return instance of ProjectedEntity with only SomeProperty and AnotherProperty set. All other properties will have default values.

WithProjectedAttributes

Specifies the attributes to project in the retrieved item. Only properties specified in properties will be retrieved. Other properties will have default values.

Contrary to AsProjection methods, WithProjectedAttributes doesn't change the type of returned entity and builder.

IScanEntityRequestBuilder<TEntity> WithProjectedAttributes(params Expression<Func<TEntity, object>>[] properties);

Parameters

  • properties: The attributes to project.

Example

builder = builder.WithProjectedAttributes(
x => x.SomeProperty,
x => x.AnotherProperty
);

After execution, this Scan request will return the original entity of the builder (in this example it's EntityClass) with only SomeProperty and AnotherProperty set. All other properties will have default values.

AsDocumets

Represents the returned items as Document.

Similarly to AsProjection<TProjection>(), this operation returns different type of builder with all the previously explained consequences.

IScanDocumentRequestBuilder<TEntity> AsDocuments();

Example

var documentBuilder = builder.AsDocuments();

Scan Execution

There are 3 versions of every query execution method: regular, projected, and document. All versions have same parameters, the only difference is entity type returned value:

  • In most cases, the original entity TEntity is returned.
  • If AsProjection<TProjection>() was used during the configuration, the execution method will contain the entity type of TProjection.
  • If AsDocuments() was used, the execution method will contain the entity type of Document.

For simplicity, this document covers only regular version of execution methods.

ToPageAsync

Executes the Scan operation and returns the page of data with pagination token.

Task<PagedResult<TEntity>> ToPageAsync(CancellationToken cancellationToken = default);

Parameters

  • cancellationToken: Token that can be used to cancel the task.

Example

var page = await builder.ToPageAsync();
var items = page.Items;
var paginationToken = page.PaginationToken;

ToResponseAsync

Executes the Scan operation and returns the deserialized response.

Task<ScanEntityResponse<TEntity>> ToResponseAsync(CancellationToken cancellationToken = default);

Parameters

  • cancellationToken: Token that can be used to cancel the task.

Example

var response = await builder.ToResponseAsync();

ToAsyncEnumerable

Executes the Scan operation and returns the result as an async enumerable, with each item in the sequence representing a single retrieved item.

IAsyncEnumerable<TEntity> ToAsyncEnumerable();

Example

await foreach(var item in builder.ToAsyncEnumerable())
{
// Do something.
}

ToPagedAsyncEnumerable

Executes the Scan operation and returns the result as an async enumerable, with each item in the sequence representing a page of DynamoDB items.

IAsyncEnumerable<IReadOnlyList<TEntity>> ToPagedAsyncEnumerable();

Example

await foreach(var page in builder.ToAsyncEnumerable())
{
var items = page.Items;
var paginationToken = page.PaginationToken;

// Do something.
}