【问题标题】:Reverse a dictionary from <Parent, IEnumerable<Child>> to <Child, IEnumerable<Parent>>将字典从 <Parent, IEnumerable<Child>> 反转为 <Child, IEnumerable<Parent>>
【发布时间】:2021-07-17 20:13:18
【问题描述】:

给定一个可枚举的 Parent,其中每个条目包含一个 IEnumerableChild 对象

IEnumerable<KeyValuePair<Parent, IEnumerable<Child>>>

我想反转它,使其成为ChildIEnumerable,其中每个条目都包含ParentIEnumerable 对象。

IEnumerable<KeyValuePair<Child, IEnumerable<Parent>>>

【问题讨论】:

    标签: c# linq


    【解决方案1】:
    source
        .SelectMany(
            x => x.Value,
            (k, v) => new
            {
                Child = k.v,
                Parent = k.Key
            })
        .GroupBy(
            keySelector: x => x.Child,
            elementSelector: x => x.Parent)
        .ToDictionary(
            keySelector: x => x.Key,
            elementSelector: x => x.AsEnumerable());
    

    【讨论】:

      猜你喜欢
      • 2018-01-02
      • 2018-03-12
      • 2016-03-22
      • 1970-01-01
      • 2019-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-05
      相关资源
      最近更新 更多