dav
dav

Reputation: 33

Merging two Arrays using LinQ

I have two arrays:

var students  = new [] {"Elisa" ,"Sarah", "Frank","Frederic"};

var votes  = new[] {90, 70, 40,80};

How can I print something like this using linq if possible?

"Elisa 90"
"Sarah 70" 
"Frank 40"
"Frederic 80"

Upvotes: 3

Views: 1355

Answers (1)

BRAHIM Kamel
BRAHIM Kamel

Reputation: 13755

You can use Linq.Zip

var students = new[] { "Elisa", "Sarah", "Frank", "Frederic" };
var votes = new[] { 90, 70, 40, 80 };
var studendsAndVotes = students.Zip(votes, (student, vote) => student + " " + vote);  

from MSDN
Applies a specified function to the corresponding elements of two sequences, producing a sequence of the results.

Upvotes: 10

Related Questions