【发布时间】:2020-08-04 21:11:04
【问题描述】:
我们有一个 API 响应体,它可以包含多个文档。在 Documents 类中,有多个带有 PropertyIds 的 Properties。
如何获取所有文档中所有 PropertiesId 的列表? SelectMany 和 Select 是否正确?这似乎在下面工作,只是想验证
var result = response.Body.Documents?.SelectMany(x=>x.Properties).Select(x=>x.PropertyId)
注意:尝试 SelectManys 时,会出现以下错误,
错误 CS0411 无法从用法推断方法“Enumerable.SelectMany(IEnumerable, Func>)”的类型参数。尝试明确指定类型参数。
【问题讨论】:
-
另一个选项是
response.Body.Documents?.SelectMany(x=>x.Properties.Select(z => z.PropertyId))。我认为你的方法更好,方法调用比我的例子少。 -
您的代码似乎正确,但需要查看
Documents类的结构、Properties和PropertyId才能确定您需要的答案。