【问题标题】:Convert a parent-child-grandchild hierarchy to another p-c-gc hierarchy将父-子-孙层次结构转换为另一个 p-c-gc 层次结构
【发布时间】:2014-01-14 09:16:02
【问题描述】:

我有一个对象列表 A。每个 A 包含一个 B 列表。每个 B 包含一个 C 列表。 我想知道是否可以在另一个相似对象列表 D 中使用 linq 以优雅的方式转换它,每个对象都包含一个 E 列表,每个对象都包含一个 F 列表。 比如说,为了这个例子,每个类只有一个数字 ID 和一个字符串描述(当然,在 A、B、D、E 的情况下还有一个孩子列表)。

【问题讨论】:

    标签: vb.net linq list


    【解决方案1】:

    假设 D、E 和 F 具有接受 ID、描述以及在 D 和 E 的情况下分别接受 B 和 C 的 IEnumerable 的构造函数,您可以使用这个:

    Dim list As List(Of A)
    Dim listSummary As List(Of D) = list.[Select](
        Function(a) New D(a.Id, a.Description, a.Children.[Select](
                          Function(b) New E(b.Id, b.Description, b.Children.[Select](
                                            Function(c) New F(c.Id, c.Description)))))).ToList()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-23
      • 1970-01-01
      • 1970-01-01
      • 2020-05-29
      • 1970-01-01
      • 2022-07-15
      • 1970-01-01
      相关资源
      最近更新 更多