C
C#15mo ago
SWEETPONY

✅ How should I use SelectAsync?

I have this:
static public async Task<IEnumerable<TResult>> SelectAsync<TSource, TResult>(
this IEnumerable<TSource> @this,
Func<TSource, Task<TResult>> selector) =>
await Task.WhenAll(@this.Select(async x => await selector(x)
.ConfigureAwait(continueOnCapturedContext: false)))
.ConfigureAwait(continueOnCapturedContext: false);
static public async Task<IEnumerable<TResult>> SelectAsync<TSource, TResult>(
this IEnumerable<TSource> @this,
Func<TSource, Task<TResult>> selector) =>
await Task.WhenAll(@this.Select(async x => await selector(x)
.ConfigureAwait(continueOnCapturedContext: false)))
.ConfigureAwait(continueOnCapturedContext: false);

What is better way to call SelectAsync:
1.
public Task<List<ReportDefinitionDto>> ToDtos(
IEnumerable<ReportDefinitionModel> models,
CultureInfo culture) =>
models.SelectAsync(model => ToDto(model, culture))
.ToList();
1.
public Task<List<ReportDefinitionDto>> ToDtos(
IEnumerable<ReportDefinitionModel> models,
CultureInfo culture) =>
models.SelectAsync(model => ToDto(model, culture))
.ToList();

2.
public Task<List<ReportDefinitionDto>> ToDtos(
IEnumerable<ReportDefinitionModel> models,
CultureInfo culture) =>
models.SelectAsync(async model => await ToDto(model, culture))
.ToList();
2.
public Task<List<ReportDefinitionDto>> ToDtos(
IEnumerable<ReportDefinitionModel> models,
CultureInfo culture) =>
models.SelectAsync(async model => await ToDto(model, culture))
.ToList();
2 Replies
boiled goose
boiled goose15mo ago
is this a mapping? do you really need it to be async?
SWEETPONY
SWEETPONY15mo ago
it is not simple mapping ToDto is async Task that do some async stuff