Skip to content

Commit 5b77186

Browse files
authored
System.Linq.Async docs (Azure#21831)
1 parent abaadc9 commit 5b77186

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

sdk/core/Azure.Core/samples/Response.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,49 @@ await foreach (Page<SecretProperties> page in allSecretProperties.AsPages())
127127
}
128128
```
129129

130+
## Using System.Linq.Async with AsyncPageable
131+
132+
The [`System.Linq.Async`](https://www.nuget.org/packages/System.Linq.Async) package provides a set of LINQ methods that operate on `IAsyncEnumerable<T>` type.
133+
Because `AsyncPageable<T>` implements `IAsyncEnumerable<T>` you can use `System.Linq.Async` to easily query and transform the data.
134+
135+
### Convert to a `List<T>`
136+
137+
`ToListAsync` can be used to convert an `AsyncPageable` to a `List<T>`. This might make several service calls if the data isn't returned in a single page.
138+
139+
```C# Snippet:SystemLinqAsyncToList
140+
AsyncPageable<SecretProperties> allSecretProperties = client.GetPropertiesOfSecretsAsync();
141+
142+
// ToListAsync would convert asynchronous enumerable into a List<T>
143+
List<SecretProperties> secretList = await allSecretProperties.ToListAsync();
144+
```
145+
146+
### Take the first N elements
147+
148+
`Take` can be used to get only the first `N` elements of the `AsyncPageable`. Using `Take` will make the fewest service calls required to get `N` items.
149+
150+
```C# Snippet:SystemLinqAsyncTake
151+
AsyncPageable<SecretProperties> allSecretProperties = client.GetPropertiesOfSecretsAsync();
152+
153+
// Take would request enough pages to get 30 items
154+
await foreach (var secretProperties in allSecretProperties.Take(30))
155+
{
156+
Console.WriteLine(secretProperties.Name);
157+
}
158+
```
159+
160+
### More methods
161+
162+
`System.Linq.Async` provides other useful methods like `Select`, `Where`, `OrderBy`, `GroupBy`, etc. that provide functionality equivalent to their synchronous [`Enumerable` counterparts](https://docs.microsoft.com/dotnet/api/system.linq.enumerable).
163+
164+
### Beware client-side evaluation
165+
166+
`System.Linq.Async` LINQ operations are executed on the client so the following query would fetch all the items just to count them:
167+
168+
```C# Snippet:SystemLinqAsyncCount
169+
// DANGER! DO NOT COPY: CountAsync as used here fetches all the secrets locally to count them.
170+
int expensiveSecretCount = await client.GetPropertiesOfSecretsAsync().CountAsync();
171+
```
172+
The same warning applies to operators like `Where`. Always prefer server-side filtering, aggregation, or projections of data if available.
130173
## Iterating over pageable
131174

132175
`Pageable<T>` is a synchronous version of `AsyncPageable<T>`, it can be used with a normal `foreach` loop.

sdk/core/Azure.Core/tests/Azure.Core.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<PackageReference Include="NUnit" />
1414
<PackageReference Include="NUnit3TestAdapter" />
1515
<PackageReference Include="Microsoft.NET.Test.Sdk" />
16+
<PackageReference Include="System.Linq.Async" />
1617
</ItemGroup>
1718
<ItemGroup>
1819
<ProjectReference Include="$(AzureCoreTestFramework)" />

sdk/core/Azure.Core/tests/samples/ResponseSamples.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.IO;
7+
using System.Linq;
78
using System.Threading.Tasks;
89
using Azure.Identity;
910
using Azure.Security.KeyVault.Secrets;
@@ -105,6 +106,52 @@ public async Task AsyncPageable()
105106
#endregion
106107
}
107108

109+
[Test]
110+
[Ignore("Only verifying that the sample builds")]
111+
public async Task AsyncPageableSystemLinqAsyncToList()
112+
{
113+
// create a client
114+
var client = new SecretClient(new Uri("http://example.com"), new DefaultAzureCredential());
115+
116+
#region Snippet:SystemLinqAsyncToList
117+
AsyncPageable<SecretProperties> allSecretProperties = client.GetPropertiesOfSecretsAsync();
118+
119+
// ToListAsync would convert asynchronous enumerable into a List<T>
120+
List<SecretProperties> secretList = await allSecretProperties.ToListAsync();
121+
#endregion
122+
}
123+
124+
[Test]
125+
[Ignore("Only verifying that the sample builds")]
126+
public async Task AsyncPageableSystemLinqAsyncTake()
127+
{
128+
// create a client
129+
var client = new SecretClient(new Uri("http://example.com"), new DefaultAzureCredential());
130+
131+
#region Snippet:SystemLinqAsyncTake
132+
AsyncPageable<SecretProperties> allSecretProperties = client.GetPropertiesOfSecretsAsync();
133+
134+
// Take would request enough pages to get 30 items
135+
await foreach (var secretProperties in allSecretProperties.Take(30))
136+
{
137+
Console.WriteLine(secretProperties.Name);
138+
}
139+
#endregion
140+
}
141+
142+
[Test]
143+
[Ignore("Only verifying that the sample builds")]
144+
public async Task AsyncPageableSystemLinqAsyncCount()
145+
{
146+
// create a client
147+
var client = new SecretClient(new Uri("http://example.com"), new DefaultAzureCredential());
148+
149+
#region Snippet:SystemLinqAsyncCount
150+
// DANGER! DO NOT COPY: CountAsync as used here fetches all the secrets locally to count them.
151+
int expensiveSecretCount = await client.GetPropertiesOfSecretsAsync().CountAsync();
152+
#endregion
153+
}
154+
108155
[Test]
109156
[Ignore("Only verifying that the sample builds")]
110157
public async Task AsyncPageableLoop()

0 commit comments

Comments
 (0)