C
C#2y 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
Omnissiah
Omnissiah2y ago
is this a mapping? do you really need it to be async?
SWEETPONY
SWEETPONYOP2y ago
it is not simple mapping ToDto is async Task that do some async stuff
Want results from more Discord servers?
Add your server