Andy Nugent
Andy Nugent

Reputation: 874

C# LINQ sum array properties

So I have a class with an array of values, and a list of those classes.

And I want to return the sum (or any other operation) of all the items in the list, also as an array.

E.g. the sum of {1,2,3}, {10,20,30} & {100,200,300} would be {111,222,333}

So, the resulting array's 1st element will be the sum of all the 1st elements in the input arrays, the 2nd element will be the sum of all the 2nd elements in the input arrays, etc.

I can do it with:

    public class Item
    {
        internal int[] Values = new int[3];
    }

    public class Items : List<Item>
    {
        internal int[] Values
        {
            get
            {
                int[] retVal = new int[3];
                for (int x = 0; x < retVal.Length; x++)
                {
                    retVal[x] = this.Sum(i => i.Values[x]);
                }
                return retVal;
            }
        }
    }

But I feel that this should be achievable as a single line using LINQ. Is it?

Upvotes: 2

Views: 1648

Answers (2)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186668

You can try to Group items withing the arrays by their indexes (so we sum all 1st arrays items, every 2nd items etc.):

int[] retVal = myList
  .SelectMany(item => item.Values
     .Select((value, index) => new {value, index}))
  .GroupBy(item => item.index, item => item.value)
  .Select(group => group.Sum())
  .ToArray();

Upvotes: 3

Zohar Peled
Zohar Peled

Reputation: 82474

Yes, this can be done using a single linq code line, using Enumrable.Range, Max, Select and Sum:

Notice I've also included a simple condition to save you from an IndexOutOfRangeException should one of the arrays is a different length than the others.

internal int[] ValuesLinq
{
    get
    {
        return Enumerable
            .Range(0, this.Max(i => i.Values.Length))
            .Select(ind => this.Sum(item => item.Values.Length > ind ? item.Values[ind] : 0))
            .ToArray();
    }
}

You can see a live demo on Rextester

Upvotes: 4

Related Questions