C#/netCoreApp3.1:异步lambda问题:无法转换hashset< tast< string>>> hashset< string>

发布于 2025-02-12 05:02:46 字数 969 浏览 0 评论 0原文

涉及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 awaited before assigning its properties to the string that is returned by the block.

Thus, it seems this block would return an IEnumerable of strings rather than of Task<string>s, since each Task generated by the Select() is (theoretically) being awaited before being evaluated.

Do you have any idea what I might be doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

西瑶 2025-02-19 05:02:47

选择任务,然后完成结果

var noteDetailsTasks = (await _api.GetContactAssocData(contactId))?
    .Results?
    .Select(r => _api.GetNoteDetails(r.ToObjectId));

var uniqueNotes = (await Task.WhenAll(noteDetailsTasks))
    .Select(noteDetail => 
        $"{noteDetail?.Properties?.Timestamp}|{noteDetail?.Properties?.NoteBody}")
    .ToHashSet();

Select tasks then handle the results when completed

var noteDetailsTasks = (await _api.GetContactAssocData(contactId))?
    .Results?
    .Select(r => _api.GetNoteDetails(r.ToObjectId));

var uniqueNotes = (await Task.WhenAll(noteDetailsTasks))
    .Select(noteDetail => 
        
quot;{noteDetail?.Properties?.Timestamp}|{noteDetail?.Properties?.NoteBody}")
    .ToHashSet();
枯叶蝶 2025-02-19 05:02:47

您不能在选择中使用异步,请在foreach中等待和附加项目
要手动进行标签。

@svyatoslav-danyliv发表的这一评论回答了我的问题:

You cannot use async in Select, do awaits in foreach and append items
to HashSet manually.

This comment posted by @svyatoslav-danyliv answered my question:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文