Elegant way to combine multiple collections of elements?

前端 未结 11 2392
再見小時候
再見小時候 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:27

    For the case when you do have a collection of collections, i.e. a List>, Enumerable.Aggregate is a more elegant way to combine all lists into one:

    var combined = lists.Aggregate((acc, list) => { return acc.Concat(list); });
    

提交回复
热议问题