c# - 从带有等待的 linq 语句中选择
问题描述
这看起来应该很简单,我有以下代码
var additionalInformation= response.AdditionalInformation.Select( async x => new AdditionalInformationItem
{
StatementCode = x?.StatementCode?.Value,
LimitDateTime = x?.LimitDateTime?.Item?.Value,
StatementTypeCode = x?.StatementTypeCode?.Value,
StatementDescription = x?.StatementDescription?.Value,
AdditionalInformationResult = await BuildAdditionalInformationPointers(x)
}).ToList();
我想要实现的是 additionalInformation 的类型
List<AdditionalInformationItem>
,我得到的是List<Task<AdditionalInformationItem>>
谁能帮我正确地重新表述我的陈述?
解决方案
您需要使用解包任务,await Task.WhenAll(additionalInformation)
然后使用additionalInformation[0].Result
.
所以是这样的:
var additionalInformation= response.AdditionalInformation.Select( async x => new AdditionalInformationItem
{
StatementCode = x?.StatementCode?.Value,
LimitDateTime = x?.LimitDateTime?.Item?.Value,
StatementTypeCode = x?.StatementTypeCode?.Value,
StatementDescription = x?.StatementDescription?.Value,
AdditionalInformationResult = await BuildAdditionalInformationPointers(x)
});
await Task.WhenAll(additionalInformation);
//This will iterate the results so may not be the most efficient method if you have a lot of results
List<AdditionalInformationItem> unwrapped = additionalInformation.Select(s => s.Result).ToList();
推荐阅读
- python - How do I increase the size of a Django form
- opencv - 无法在 Raspberry Pi 上的 NCS2 上处理 openvino dnn 识别器
- javascript - 如何在没有特定类型的情况下更新 Angular 管道
- php - 试图了解如何使用 Dark Sky API 调用未来预测
- autocomplete - 材质-UI
与选项值不同的标签 - python - Altair:用于分层绘图的两个独立滑块
- php - 关于如何在不使用 PHP 中的表单的情况下将点击的数字从数组传递到另一个页面的任何想法?
- python - pyglet 绘制黑线而不是绿色像素的问题
- ios - 将单元格从委托传递到 UITableViewController 时 indexPath 为零
- c# - Linqpad 使用丢弃(_ = 下划线)引发错误 CS0246 - 找不到类型或命名空间名称“_”