【问题标题】:C#: Find All Members under Multiple Classes with LinqC#:使用 Linq 查找多个类下的所有成员
【发布时间】: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 类的结构、PropertiesPropertyId 才能确定您需要的答案。

标签: c# .net linq .net-core


【解决方案1】:

你需要两个 selectMany

试试这个

var result = response.Body.Documents?.SelectMany(x=>x.Properties.SelectMany(j=>j.PropertyId))

【讨论】:

  • 对不起,我把第二个 selectmany 放在了错误的位置,我的回复已被编辑,现在试试。
猜你喜欢
  • 2011-09-10
  • 2022-08-17
  • 1970-01-01
  • 1970-01-01
  • 2023-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多