Async await in linq select

后端 未结 7 2306
醉酒成梦
醉酒成梦 2020-12-02 05:30

I need to modify an existing program and it contains following code:

var inputs = events.Select(async ev => await ProcessEventAsync(ev))
                          


        
相关标签:
7条回答
  • 2020-12-02 06:04

    "Just because you can doesn't mean you should."

    You can probably use async/await in LINQ expressions such that it will behave exactly as you want it to, but will any other developer reading your code still understand its behavior and intent?

    (In particular: Should the async operations be run in parallel or are they intentionally sequential? Did the original developer even think about it?)

    This is also shown clearly by the question, which seems to have been asked by a developer trying to understand someone else's code, without knowing its intent. To make sure this does not happen again, it may be best to rewrite the LINQ expression as a loop statement, if possible.

    0 讨论(0)
提交回复
热议问题