c# - Async Await In Linq .TakeWhile -
i trying convert foreach function linq function
here normal code [works fine]
var tlist = new list<func<task<bool>>> { method1, method2 }; tlist.shuffle(); int succeed = 0; foreach (var task in tlist) { var result = await task(); if(!result) break; succeed += 1; } messagebox.show(succeed == 1 ? "loading complete." : "something went wrong!");
and here after converted linq [giving 2 compiler errors]
var tlist = new list<func<task<bool>>> { method1, method2 }; tlist.shuffle(); int succeed = tlist.select(async task => await task()).takewhile(result => result).count(); messagebox.show(succeed == 1 ? "loading complete." : "something went wrong!");
errors
- cannot convert lambda expression delegate type 'system.func,bool>' because of return types in block not implicitly convertible the
delegate return type- cannot implicitly convert type 'system.threading.tasks.task' 'bool'
i wonder why compiler giving messages, appreciated.
note : tried .takewhile(async result => await result)
error
- the return type of async method must void, task, or task t
method1 , method2 if wanna them :
public async task<bool> method1() { await task.delay(1000); console.writeline("method1"); return false; } public async task<bool> method2() { await task.delay(1000); console.writeline("method2"); return true; }
this because cannot convert task<bool>
bool
.. compiler has instructed.
change this:
.takewhile(result => result)
..to this:
.takewhile(result => result.result)
Comments
Post a Comment