ReadSensorsAsync
有时会返回null Result
。
var tasksRead = Enumerable.Range(3, 35).Select(i => ReadSensorsAsync(i));
var tasksRecord = tasksRead.Select(x => RecordReadingAsync(x.Result));
如何在第二次查询中过滤掉这些任务,我只处理非空项?
答案 0 :(得分:4)
您只需在第二个语句中添加Where:
var tasksRead = Enumerable
.Range(3, 35)
.Select(i => ReadSensorsAsync(i));
var tasksRecord = tasksRead
.Where(x => x.Result != null)
.Select(x => RecordReadingAsync(x.Result));
答案 1 :(得分:1)
试试这个:
var tasksRecord = from tr in Enumerable.Range(3, 35).Select(ReadSensorsAsync)
where tr != null select RecordReadingAsync(tr.Result);