用于迭代项目集合的通用适配器
A generic adapter to iterate over a Collection of Collection of items
我正在寻找一个通用的 class 适配器,它使我能够通过单个迭代器迭代对象的集合(IEnumerable 的 IEnumerable)。
换句话说:如何通过让用户将其显示为一个单一的平面集合来伪造集合。
示例:
对于这样的结构:
public class Signal
{
Point[] points;
}
public class Analysis
{
List<Signal> Signals;
}
我想要一个通用适配器,使我能够迭代 Analysis 对象的每个点。
适配器将使我能够做到:
var analysisInnerCollectionOfPointAdapter =
new GenericCollectionOfCollectionAdapterIterator(myAnalysis);
foreach(Point point in AnalysisInnerCollectionOfPointAdapter)
{
...
}
我不想创建任何重复的集合。我只想从我的迭代中抽象出内部步骤(集合的集合)。
我刚开始尝试对其进行编程,但我想知道是否没有已经存在的东西可能比我未来可能创造的更好。也许在像 Linq 或 MoreLinq 这样的现有库中?也许您已经做到了并且有一些不错的东西?
编辑(一小时后):
我刚刚发现:Difference Between Select and SelectMany 很好地说明了我想要什么。感谢 AlexeiLevenkov。
几个月后...AlexeiLevenko 没有 post :
SelectMany does not create any copies - it is similar to most other LINQ operations like Select/Where... Not sure what code you have in mind saying "copy of the data" (I don't believe you can avoid copying value types, but it does no look like OP needs to avoid that)...
analysis.Signal.SelectMAny(x=>x)
only copies Point
assuming it is value types...
我正在寻找一个通用的 class 适配器,它使我能够通过单个迭代器迭代对象的集合(IEnumerable 的 IEnumerable)。
换句话说:如何通过让用户将其显示为一个单一的平面集合来伪造集合。
示例: 对于这样的结构:
public class Signal
{
Point[] points;
}
public class Analysis
{
List<Signal> Signals;
}
我想要一个通用适配器,使我能够迭代 Analysis 对象的每个点。
适配器将使我能够做到:
var analysisInnerCollectionOfPointAdapter =
new GenericCollectionOfCollectionAdapterIterator(myAnalysis);
foreach(Point point in AnalysisInnerCollectionOfPointAdapter)
{
...
}
我不想创建任何重复的集合。我只想从我的迭代中抽象出内部步骤(集合的集合)。
我刚开始尝试对其进行编程,但我想知道是否没有已经存在的东西可能比我未来可能创造的更好。也许在像 Linq 或 MoreLinq 这样的现有库中?也许您已经做到了并且有一些不错的东西?
编辑(一小时后): 我刚刚发现:Difference Between Select and SelectMany 很好地说明了我想要什么。感谢 AlexeiLevenkov。
几个月后...AlexeiLevenko 没有 post
SelectMany does not create any copies - it is similar to most other LINQ operations like Select/Where... Not sure what code you have in mind saying "copy of the data" (I don't believe you can avoid copying value types, but it does no look like OP needs to avoid that)...
analysis.Signal.SelectMAny(x=>x)
only copiesPoint
assuming it is value types...