How to concatenate two collections by index in LINQ

后端 未结 3 612
情深已故
情深已故 2021-02-12 08:15

What could be a LINQ equivalent to the following code?

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


        
3条回答
  •  说谎
    说谎 (楼主)
    2021-02-12 08:52

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

提交回复
热议问题