Rotate - Transposing a List> using LINQ C#

前端 未结 6 1072
半阙折子戏
半阙折子戏 2020-11-29 11:12

I\'m having a List>, which is return from the remote data source (i.e., WCF). So, I need to modify the following data into a user-frien

6条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-29 11:58

    This is a simple and flexible solution, it will handle multiple inner lists with any number of dimensions.

    List> PersonInfo = new List>()
    {
        new List() {"John", "Peter", "Watson"},
        new List() {"1000", "1001", "1002"}
    };
    
    
    var result = PersonInfo
        .SelectMany(inner => inner.Select((item, index) => new { item, index }))
        .GroupBy(i => i.index, i => i.item)
        .Select(g => g.ToList())
        .ToList();
    

提交回复
热议问题