How to concatenate two collections by index in LINQ

后端 未结 2 486
被撕碎了的回忆
被撕碎了的回忆 2021-02-12 08:01

What could be a LINQ equivalent to the following code?

string[] values = { \"1\", \"hello\", \"true\" };
Type[] types = { typeof(int), typeof(string), typeof(b         


        
2条回答
  •  执念已碎
    2021-02-12 09:08

    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();
    

提交回复
热议问题