I basically want to go through the 2d List<> and deposit the sum of the items in a column to a 1d List. So far have a double for loop but I cant figure out the logic. Any help with how I can start to contruct it logically would be helpful. Thank you. This is the code I have so far:
Asked
Active
Viewed 56 times
-2
-
Hello and welcome to SO! To improve your chances of getting an answer please read [How do I ask a good question](https://stackoverflow.com/help/how-to-ask) and please update your post to include a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – Trevor Nov 16 '21 at 20:46
-
Questions that ask "where do I start?" are typically too broad and are not a good fit for this site. People have their own method for approaching the problem and because of this there cannot be a _correct_ answer. Give a good read over [Where to Start](//softwareengineering.meta.stackexchange.com/a/6367) and [edit] your post. – gunr2171 Nov 16 '21 at 20:47
-
Post how you trying to do and the incorrect results you have. – Henryk Budzinski Nov 16 '21 at 21:11
-
Thank you for ur input. I edited my code so hopefully its clearer now. – chicacherry Nov 16 '21 at 21:56
1 Answers
1
So if you have this:
var grid = new List<List<int>> {
new() { 1,2 },
new() { 3,4 }
}
You can do:
grid.Pivot().Select(c => c.Sum());
And you'll get an enumerable that is { 4, 6 }
(1+3 and 2+4)
You can get Pivot from fubo's answer here (or choose any one of the other suggested transforms)
If I misunderstood and you wanted {3,5} just skip the Pivot part

Caius Jard
- 72,509
- 5
- 49
- 80