How to apply a function to every element in a list using Linq in C #, like the reduce () method in python?

How to apply a function to every element in a list using Linq in C #, like reduce () method in python?

+3
source share
2 answers

Assuming you say this reduction function , the equivalent in C # and LINQ is Enumerable.Aggregate .

Quick example:

var list = Enumerable.Range(5, 3); // [5, 6, 7]
Console.WriteLine("Aggregation: {0}", list.Aggregate((a, b) => (a + b)));
// Result is "Aggregation: 18"
+11
source

Enumerable.Aggregate - . (, , ) == > list.Aggregate(, ). , "", Sum, Min, Max, Average .. Aggregate , .

+2

Source: https://habr.com/ru/post/1767241/


All Articles