【发布时间】:2013-03-13 15:49:14
【问题描述】:
我希望能够做这样的事情来建立一个 网状数据结构。
IReadOnlyList<Point> points;
IReadOnlyList<IReadOnlyList<int>> triangles;
其中三角形是点列表的索引。给定一个索引 我们可以很容易地找到三角形''ti''的点
IEnumerable<Point> points = triangles[ti].Select(pi=>points[pi])
但是我希望能够定义一个方便的结构
IReadOnlyList<IReadOnlyList<Point>> trianglesAsPoints;
这样我就可以了
IEnumerable<Point> points = triangles[ti]
这样做的明显方法是创建一个类似于选择器的 linq
IReadOnlyList<T> Select( this IReadOnlyList<U> This
, Func<U,T> selector)
它返回一个实例,其类覆盖以下方法和 调用选择器
public interface IReadOnlyList<out T> : IReadOnlyCollection<T>, IEnumerable<T>, IEnumerable
{
// Summary:
// Gets the element at the specified index in the read-only list.
//
// Parameters:
// index:
// The zero-based index of the element to get.
//
// Returns:
// The element at the specified index in the read-only list.
T this[int index] { get; }
}
这种模式的标准库或 nuget 中是否存在这样的工厂? 注意我不希望 IEnumerable 结果因为我会失去索引能力 和 Count 属性,我只想懒惰地转换值,这意味着 not 将所有值复制到前面的新列表实例中。
【问题讨论】: