将KeyValuePair <int,string>转换为int []数组和string []数组

Ram*_*mie 1 .net c# jagged-arrays multidimensional-array

KeyValuePair<int, string>[][] partitioned = SplitVals(tsRequests.ToArray());
Run Code Online (Sandbox Code Playgroud)

不要太担心我使用的方法; 我只想说我得到一个参差不齐的KeyValuePairs数组,这些数组被分成不同的数组.

foreach (KeyValuePair<int, string>[] pair in partitioned)
{
    foreach (KeyValuePair<int, string> k in pair)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

我需要知道如何有效地获取int数组中的int,以及来自keyvaluepairs数组的单独字符串数组中的字符串.这样两个索引在单独的数组中相互匹配.

例如,在我将其拆分为int []数组和string []数组之后,

intarray[3] must match stringarray[3] just like it did in the keyvaluepair.
Run Code Online (Sandbox Code Playgroud)

让我说我有一个与KVP的锯齿状阵列,如:

    [1][]<1,"dog">, <2,"cat">
    [2][]<3,"mouse">,<4,"horse">,<5,"goat">
    [3][]<6,"cow">
Run Code Online (Sandbox Code Playgroud)

我需要在每次迭代期间进行此操作

    1. 1,2 / "dog","cat"
    2. 3,4,5 / "mouse", "horse", "goat"
    3. 6 / "cow"
Run Code Online (Sandbox Code Playgroud)

Ser*_*rvy 6

int[] keys = partitioned.Select(pairs => pairs.Select(pair => pair.Key).ToArray())
    .ToArray();
string[] values = partitioned.Select(pairs => pairs.Select(pair => pair.Value).ToArray())
    .ToArray();
Run Code Online (Sandbox Code Playgroud)

  • @Ramie你想要的是两个带有键和值的锯齿状数组; 不扁平.这是一个微不足道的变化; 你只需要使用`Select`而不是`SelectMany`. (2认同)