说我有这样的方法:
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,它可以展平嵌套的枚举循环。它让我疯了,因为我知道我以前做过这个,但我似乎无法记住它是什么。有任何提示吗?
答案 0 :(得分: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
,所有儿童,所有儿童等等。
如果您来自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);
所有这些扩展方法都是迭代的,因此不受堆栈大小的限制。
我有很多这些类型的方法,包括预购和后期深度优先遍历,如果你想看到它们,我会制作一个回购并上传它们:)
答案 1 :(得分: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);
}
答案 2 :(得分:0)
效率不高,但你可以使用它:
List<Record> rcrdList = new List<Record>();
foreach (var item in someItems)
{
rcrdList.AddRange(item.GetSomeRecords());
}
return rcrdList;