Elegant way to combine multiple collections of elements?

前端 未结 11 2405
再見小時候
再見小時候 2020-12-05 03:36

Say I have an arbitrary number of collections, each containing objects of the same type (for example, List foo and List bar).

11条回答
  •  广开言路
    2020-12-05 04:30

    You could always use Aggregate combined with Concat...

            var listOfLists = new List>
            {
                new List {1, 2, 3, 4},
                new List {5, 6, 7, 8},
                new List {9, 10}
            };
    
            IEnumerable combined = new List();
            combined = listOfLists.Aggregate(combined, (current, list) => current.Concat(list)).ToList();
    

提交回复
热议问题