Skip to content

LINQ SelectAggregate #26945

@Thaina

Description

@Thaina

I wish we could have select that could also keep state of aggregation and continue the iteration, send the aggregation to the next select

It should be simple to add this

public static IEnumerable<V> SelectAggregate<T,V>(this IEnumerable<T> items,V seed,Func<V,T,V> func)
{
	foreach(var item in items)
	{
		seed	= func(seed,item);
		yield return seed;
	}
}

////

var array = new int[]{ 1,2,3 };
var sumArray = array.SelectAggregate(0,(seed,item) => seed + item).ToArray();
// { 1,3,6 }

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-System.Linq

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions