How to append enumerable collection to an existing list in C#

前端 未结 2 849
灰色年华
灰色年华 2021-02-04 01:51

i have three functions which are returning an IEnumerable collection. now i want to combine all these into one List. so, is there any method by which i can append items from IEn

2条回答
  •  灰色年华
    2021-02-04 02:39

    Well, something will have to loop... but in LINQ you could easily use the Concat and ToList extension methods:

    var bigList = list1.Concat(list2).Concat(list3).ToList();
    

    Note that this will create a new list rather than appending items to an existing list. If you want to add them to an existing list, List.AddRange is probably what you're after:

    bigList.AddRange(list1);
    bigList.AddRange(list2);
    bigList.AddRange(list3);
    

提交回复
热议问题