3

假设我有这样的方法:

IEnumerable<record> GetSomeRecords()
{
  while(...)
  {
    yield return aRecord
  }
}

现在,假设我有一个调用者,它也返回一个相同类型的枚举,像这样

IEnumerable<record> ParentGetSomeRecords()
{
  // I want to do this, but for some reason, my brain locks right here
  foreach(item in someItems)
    yield return GetSomeRecords();
}

该代码出现语法错误错误,因为 yield return 需要一个类型记录,而我正在返回一个 IEnumerable 记录

我想要一个“扁平” IEnumerable 来扁平化一个嵌套的枚举循环。这让我发疯,因为我知道我以前做过,但我似乎不记得那是什么了。有任何提示吗?

4

3 回答 3

9

这就是你所追求的吗?

IEnumerable<record> ParentGetSomeRecords()
{
    foreach(var item in someItems)
        foreach(var record in GetSomeRecords())
            yield return record;
}

如前所述,这仅适用于单个级别的子级,但与您的示例代码最等效。

更新

有些人似乎认为您想要扁平化层次结构的能力。这是一个执行广度优先展平的扩展方法(在孩子之前获取兄弟姐妹):

来自单个项目:

[Pure]
public static IEnumerable<T> BreadthFirstFlatten<T>(this T source, Func<T, IEnumerable<T>> selector)
{
    Contract.Requires(!ReferenceEquals(source, null));
    Contract.Requires(selector != null);
    Contract.Ensures(Contract.Result<IEnumerable<T>>() != null);

    var pendingChildren = new List<T> {source};

    while (pendingChildren.Any())
    {
        var localPending = pendingChildren.ToList();
        pendingChildren.Clear();
        foreach (var child in localPending)
        {
            yield return child;
            var results = selector(child);
            if (results != null)
                pendingChildren.AddRange(results);
        }
    }
}

这可以像这样使用:

record rec = ...;
IEnumerable<record> flattened = rec.BreadthFirstFlatten(r => r.ChildRecords);

这将导致IEnumerable<record>包含rec所有 recs 子项、所有子项的子项等。

如果您来自 的集合records,请使用以下代码:

[Pure]
private static IEnumerable<T> BreadthFirstFlatten<T, TResult>(IEnumerable<T> source, Func<T, TResult> selector, Action<ICollection<T>, TResult> addMethod)
{
    Contract.Requires(source != null);
    Contract.Requires(selector != null);
    Contract.Requires(addMethod != null);
    Contract.Ensures(Contract.Result<IEnumerable<T>>() != null);

    var pendingChildren = new List<T>(source);

    while (pendingChildren.Any())
    {
        var localPending = pendingChildren.ToList();
        pendingChildren.Clear();
        foreach (var child in localPending)
        {
            yield return child;
            var results = selector(child);
            if (!ReferenceEquals(results, null))
                addMethod(pendingChildren, results);
        }
    }
}

[Pure]
public static IEnumerable<T> BreadthFirstFlatten<T>(this IEnumerable<T> source, Func<T, IEnumerable<T>> selector)
{
    Contract.Requires(source != null);
    Contract.Requires(selector != null);
    Contract.Ensures(Contract.Result<IEnumerable<T>>() != null);

    return BreadthFirstFlatten(source, selector, (collection, arg2) => collection.AddRange(arg2));
}

[Pure]
public static IEnumerable<T> BreadthFirstFlatten<T>(this IEnumerable<T> source, Func<T, T> selector)
{
    Contract.Requires(source != null);
    Contract.Requires(selector != null);
    Contract.Ensures(Contract.Result<IEnumerable<T>>() != null);

    return BreadthFirstFlatten(source, selector, (collection, arg2) => collection.Add(arg2));
}

这两种扩展方法可以像这样使用:

IEnumerable<records> records = ...;
IEnumerable<record> flattened = records.BreadthFirstFlatten(r => r.ChildRecords);

或者从相反的方向:

IEnumerable<record> records = ...;
IEnumerable<record> flattened = records.BreadthFirstFlatten(r => r.ParentRecords);

所有这些扩展方法都是迭代的,因此不受堆栈大小的限制。

我有一大堆这些类型的方法,包括预排序和后排序深度优先遍历,如果你想看到它们,我会做一个 repo 并上传到某个地方:)

于 2013-02-28T21:58:09.887 回答
2

怎么样:

IEnumerable<record> ParentGetSomeRecords()
{
    var nestedEnumerable = <whatever the heck gets your nested set>;
    // SelectMany with an identity flattens 
    // IEnumerable<IEnumerable<T>> to just IEnumerable<T>
    return nestedEnumerable.SelectMany(rec => rec);
}
于 2013-02-28T21:55:38.200 回答
0

效率低下,但你可以使用这个:

List<Record> rcrdList = new List<Record>();
foreach (var item in someItems)
{
    rcrdList.AddRange(item.GetSomeRecords());
}
return rcrdList;
于 2013-02-28T21:58:01.813 回答