Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq - How can a add the contents of two list

Tags:

c#

linq

I have two IEnumberable<double> list.

How can i add the each value of one list to the value in the same position in the other list using Linq?

like image 709
theHaggis Avatar asked Dec 17 '22 06:12

theHaggis


1 Answers

You can use Enumerable.Zip to do this:

var results = first.Zip(second, (f,s) => f+s);

Note that, if the lists aren't the same length, your results will be the length of the shorter of the two lists (Zip as written above will add together elements until one sequence ends, then stop...)

like image 83
Reed Copsey Avatar answered Dec 18 '22 20:12

Reed Copsey