What could be a LINQ equivalent to the following code?
string[] values = { "1", "hello", "true" };
Type[] types = { typeof(int), typeof(string), typeof(bool) };
object[] objects = new object[values.Length];
for (int i = 0; i < values.Length; i++)
{
objects[i] = Convert.ChangeType(values[i], types[i]);
}
.NET 4 has a Zip operator that lets you join two collections together.
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.