Skip to content

Commit 0e13bba

Browse files
Update code snippets
1 parent 76636e1 commit 0e13bba

File tree

1 file changed

+40
-12
lines changed

1 file changed

+40
-12
lines changed

articles/storage/tables/table-storage-design-patterns.md

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -658,11 +658,9 @@ using System.Linq;
658658
using Azure.Data.Tables
659659
```
660660

661-
The employeeTable is a CloudTable object that implements a CreateQuery\<ITableEntity>() method, which returns a TableQuery\<ITableEntity>. Objects of this type implement an IQueryable and allow using both LINQ Query Expressions and dot notation syntax.
661+
Retrieving multiple entities can be achieved by specifying a query with a **filter** clause. To avoid a table scan, you should always include the **PartitionKey** value in the filter clause, and if possible the **RowKey** value to avoid table and partition scans. The table service supports a limited set of comparison operators (greater than, greater than or equal, less than, less than or equal, equal, and not equal) to use in the filter clause.
662662

663-
Retrieving multiple entities and be achieved by specifying a query with a **filter** clause. To avoid a table scan, you should always include the **PartitionKey** value in the filter clause, and if possible the **RowKey** value to avoid table and partition scans. The table service supports a limited set of comparison operators (greater than, greater than or equal, less than, less than or equal, equal, and not equal) to use in the filter clause.
664-
665-
The following C# code snippet finds all the employees whose last name starts with "B" (assuming that the **RowKey** stores the last name) in the sales department (assuming the **PartitionKey** stores the department name):
663+
In the following example, `employeeTable` is a [TableClient](/dotnet/api/azure.data.tables.tableclient) object. This example finds all the employees whose last name starts with "B" (assuming that the **RowKey** stores the last name) in the sales department (assuming the **PartitionKey** stores the department name):
666664

667665
```csharp
668666
var employees = employeeTable.Query<EmployeeEntity>(e => (e.PartitionKey == "Sales" && e.RowKey.CompareTo("B") >= 0 && e.RowKey.CompareTo("C") < 0));
@@ -700,20 +698,50 @@ foreach (var emp in employees)
700698
}
701699
```
702700

703-
The following C# code handles continuation tokens explicitly:
701+
You can also specify the maximum number of entities that are returned per page. The following example shows how to query entities with `maxPerPage`:
704702

705703
```csharp
706-
TableContinuationToken continuationToken = null;
707-
do
704+
var employees = employeeTable.Query<EmployeeEntity>(maxPerPage: 10);
705+
706+
// Iterate the Pageable object by page
707+
foreach (var page in employees.AsPages())
708708
{
709-
var employees = employeeTable.Query<EmployeeEntity>("PartitionKey eq 'Sales'");
710-
foreach (var emp in employees.AsPages())
709+
// Iterate the entities returned for this page
710+
foreach (var emp in page.Values)
711+
{
712+
// ...
713+
}
714+
}
715+
```
716+
717+
In more advanced scenarios, you may want to store the continuation token returned from the service so that your code controls exactly when the next pages is fetched. The following example shows a basic scenario of how the token can be fetched and applied to paginated results:
718+
719+
```csharp
720+
string continuationToken = null;
721+
bool moreResultsAvailable = true;
722+
while (moreResultsAvailable)
723+
{
724+
var page = employeeTable
725+
.Query<EmployeeEntity>()
726+
.AsPages(continuationToken, pageSizeHint: 10)
727+
.FirstOrDefault(); // pageSizeHint limits the number of results in a single page, so we only enumerate the first page
728+
729+
if (page == null)
730+
break;
731+
732+
// Get the continuation token from the page
733+
// Note: This value can be stored so that the next page query can be executed later
734+
continuationToken = page.ContinuationToken;
735+
736+
var pageResults = page.Values;
737+
moreResultsAvailable = pageResults.Any() && continuationToken != null;
738+
739+
// Iterate the results for this page
740+
foreach (var result in pageResults)
711741
{
712742
// ...
713-
continuationToken = emp.ContinuationToken;
714743
}
715-
716-
} while (continuationToken != null);
744+
}
717745
```
718746

719747
By using continuation tokens explicitly, you can control when your application retrieves the next segment of data. For example, if your client application enables users to page through the entities stored in a table, a user may decide not to page through all the entities retrieved by the query so your application would only use a continuation token to retrieve the next segment when the user had finished paging through all the entities in the current segment. This approach has several benefits:

0 commit comments

Comments
 (0)