Effective column operations (sum, average ...) on a very large Enumerable <arrayType []> with Linq

Assuming we have the following IEnumerable example:

IEnumerable<int[]> veryLargeJaggedArray =
{
   new int[] {1, 3, 5},
   new int[] {0, 2, 4},
   new int[] {11,22,6},
   ...lots of data streaming in
}

where the base collection is not based on ICollection (i.e. there is no quick search for Count ()), what is the most efficient way to use C # Linq to perform aggregate type column operations?

This question extends my previous question in a more practical case.

-1
source share
2 answers

You can easily create your own method to generate a sequence representing the column, given that the input:

public static IEnumerable<T> GetColumn<T>(
    this IEnumerable<IList<T>> data, 
    int columnNumber)
{
    return data.Select(row => row[columnNumber]);
}

Now you can write the code, for example:

var firstColumnSum = veryLargeJaggedArray.GetColumn(0).Sum();
var secondColumnAverage = veryLargeJaggedArray.GetColumn(1).Average();
+1

. - :

var result = MyCollection.Sum();

var result = MyCollection.Average(i => i.NumericProperty);

, IList<T>, ( Count) , IEnumerable<T>, IEnumerable<T>.

0

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


All Articles