How to “zip” or “rotate” a variable number of lists?

后端 未结 8 1631
广开言路
广开言路 2020-11-27 20:47

If I have a list containing an arbitrary number of lists, like so:

var myList = new List>()
{
    new List() { \"a\",          


        
8条回答
  •  醉梦人生
    2020-11-27 21:13

    You can do this by using the Select extension taking a Func:

    var rotatedList = myList.Select(inner => inner.Select((s, i) => new {s, i}))
                            .SelectMany(a => a)
                            .GroupBy(a => a.i, a => a.s)
                            .Select(a => a.ToList()).ToList();
    

    This will give you another List>.

    Breakdown

    .Select(inner => inner.Select((s, i) => new {s, i}))
    

    For each inner list, we project the list's content to a new anonymous object with two properties: s, the string value, and i the index of that value in the original list.

    .SelectMany(a => a)
    

    We flatten the result to a single list

    .GroupBy(a => a.i, a => a.s)
    

    We group by the i property of our anonymous object (recall this is the index) and select the s property as our values (the string only).

    .Select(a => a.ToList()).ToList();
    

    For each groups, we changed the enumerable to a list and another list for all the groups.

提交回复
热议问题