IEnumerable 的重载之一是:
public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, int, TResult> selector);
在选择器中,我希望包含源代码。我知道这听起来违反直觉,因为您首先提供了 Select 的源代码,但 JavaScript 也有类似的东西。我想在这里像这样的快速情况下使用它:
var greetings = new List<string> { "John", "Keith", "Sarah", "Matt" }.Select((name, index, source) => {
if (name == source.First())
return $"{name} (Todays Winner)";
return name;
});
上面会报错,因为 Select 的 selector 参数没有返回 3 个值。只是当前对象和索引。我希望它包括来源。
我不想先单独创建列表,然后再对其执行 .first 。
这是我在扩展方面走了多远;我不确定如何实现它。
public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, int, TResult, IEnumerable<TSource>> selector)
{
//not sure what to put in here, must be missing something simple ;(
}
更新
上述情况只是一个虚构的例子。我的实际情况需要使用.Last()
not.First()
所以索引不会有用,因为我们不知道最后一个索引是什么,而不是第一个是零。因此,我需要将源传回。