score:28

Accepted answer

.net 4 has a zip operator that lets you join two collections together.

var values = { "1", "hello", "true" };
var types = { typeof(int), typeof(string), typeof(bool) };
var objects = values.zip(types, (val, type) => convert.changetype(val, type));

the .zip method is superior to .select((s, i) => ...) because .select will throw an exception when your collections don't have the same number of elements, whereas .zip will simply zip together as many elements as it can.

if you're on .net 3.5, then you'll have to settle for .select, or write your own .zip method.

now, all that said, i've never used convert.changetype. i'm assuming it works for your scenario, so i'll leave that be.

score:5

object[] objects = values.select((s,i) => convert.changetype(s, types[i]))
                         .toarray();

score:6

assuming both arrays have the same size:

string[] values = { "1", "hello", "true" };
type[] types = { typeof(int), typeof(string), typeof(bool) };

object[] objects = values
    .select((value, index) => convert.changetype(value, types[index]))
    .toarray();

Related Query