How do I sum a list<> of arrays

后端 未结 7 1493
故里飘歌
故里飘歌 2021-02-04 08:24

I have a List< int[] > myList, where I know that all the int[] arrays are the same length - for the sake of argument, let us say I have 500 arrays, each is 2048 elements long

7条回答
  •  情话喂你
    2021-02-04 08:50

    Edit: Ouch...This became a bit harder while I wasn't looking. Changing requirements can be a real PITA.

    Okay, so take each position in the array, and sum it:

    var sums = Enumerable.Range(0, myList[0].Length)
               .Select(i => myList.Select(
                         nums => nums[i]
                      ).Sum()
               );
    

    That's kind of ugly...but I think the statement version would be even worse.

提交回复
热议问题