Bob*_*orn 1 c# linq async-await
如果我将代码分成两行,为什么会这样工作:
List<Foo> foos = await _repo.GetFoos();
foos = foos.Where(x => x.FooType != FooType.A).ToList();
Run Code Online (Sandbox Code Playgroud)
但是当我组合它们并Where在同一条线上执行时不起作用?
List<Foo> foos = await _repo.GetFoos().Where(x => x.FooType != FooType.A).ToList();
Run Code Online (Sandbox Code Playgroud)
这会产生一个错误:
Task<List<Foo>>不包含“Where”的定义...
你应该这样使用await:
List<Foo> foos = (await _repo.GetFoos()).Where(x => x.FooType != FooType.A).ToList();
Run Code Online (Sandbox Code Playgroud)
那是因为.Where()inawait _repo.GetFoos().Where()应用于_repo.GetFoos()not result of await _repo.GetFoos()。