简体   繁体   English

任务不包含如果在一行代码中完成的位置的定义

[英]Task Does Not Contain a Definition for Where If Done in One Line of Code

Why does this work if I split the code into two lines:如果我将代码分成两行,为什么会这样工作:

List<Foo> foos = await _repo.GetFoos();
foos = foos.Where(x => x.FooType != FooType.A).ToList();

But doesn't work when I combine them and do the Where on the same line?但是当我组合它们并在同一行执行Where时不起作用?

List<Foo> foos = await _repo.GetFoos().Where(x => x.FooType != FooType.A).ToList();

This produces an error:这会产生一个错误:

Task<List<Foo>> does not contain a definition for 'Where`... Task<List<Foo>>不包含 'Where` 的定义...

You should use await like this:你应该像这样使用await

List<Foo> foos = (await _repo.GetFoos()).Where(x => x.FooType != FooType.A).ToList();

That's because .Where() in await _repo.GetFoos().Where() is applied to _repo.GetFoos() not result of await _repo.GetFoos() .那是因为await _repo.GetFoos().Where().Where()应用于_repo.GetFoos()而不是await _repo.GetFoos()

The await is applied to the object returned by the entire expression. await应用于整个表达式返回的对象。

You are attempting to use Enumerable.Where on the Task returned by _repo.GetFoos which is invalid.您正试图在_repo.GetFoos返回的无效Task上使用Enumerable.Where

You could enclose the await _repo.GetFoos() in parentheses to force that to be evaluated first, then Enumerable.Where would be performed on the resultant IEnumerable .您可以将await _repo.GetFoos()括在括号中以强制首先对其进行评估,然后对结果IEnumerable执行Enumerable.Where

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM