C#/netCoreApp3.1:异步lambda问题:无法转换hashset< tast< string>>> hashset< string>
涉及async
lambda函数的代码的一部分正在失败。这是片段:
var uniqueNotes = (await _api.GetContactAssocData(contactId))?
.Results?
.Select(async r =>
{
var noteDetail = await _api.GetNoteDetails(r.ToObjectId);
return $"{noteDetail?.Properties?.Timestamp}|{noteDetail?.Properties?.NoteBody}";
}).ToHashSet();
我已经匿名化了方法/变量名称以保护我的雇主的IP。
编译器给出的错误是无法隐式转换类型'System.Collections.generic.hashset< system.threading.tasks.tasks.task< string>>'''到'system.generics.hashset< string>'
此错误消息将使完全有意义 如果 i,我未能等待
任务
在语句块中。但是,如您所见,i 出现可以正确地安装一切,以返回我的陈述:lambda开始时有一个async
在将其属性分配给块返回的字符串之前,我的块是等待
。
因此,似乎此块将返回 of
string
s而不是task< string>
s的 s,由于select> Select()
是(理论上)是等待
在评估之前。
您知道我可能做错了什么吗?
A portion of my code involving an async
Lambda function is failing. Here is the snippet:
var uniqueNotes = (await _api.GetContactAssocData(contactId))?
.Results?
.Select(async r =>
{
var noteDetail = await _api.GetNoteDetails(r.ToObjectId);
return quot;{noteDetail?.Properties?.Timestamp}|{noteDetail?.Properties?.NoteBody}";
}).ToHashSet();
I have anonymized the method/variable names to protect my employer's IP.
The error given by the compiler is Cannot implicitly convert type 'System.Collections.Generic.HashSet<System.Threading.Tasks.Task<string>>' to 'System.Generics.HashSet<string>'
This error message would make total sense if I were failing to await
the Task
inside the statement block. But as you can see, I appear to have everything correctly in place for my statement to return: there is an async
at the start of the Lambda, and the method call inside my block is await
ed before assigning its properties to the string that is returned by the block.
Thus, it seems this block would return an IEnumerable
of string
s rather than of Task<string>
s, since each Task
generated by the Select()
is (theoretically) being await
ed before being evaluated.
Do you have any idea what I might be doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
选择任务,然后完成结果
Select tasks then handle the results when completed
@svyatoslav-danyliv发表的这一评论回答了我的问题:
This comment posted by @svyatoslav-danyliv answered my question: