카테고리 없음
[.net] .NET : 두 개의 일반 목록 결합
행복을전해요
2020. 12. 30. 00:15
This should do the trick
List<Type> list1;
List<Type> list2;
List<Type> combined;
combined.AddRange(list1);
combined.AddRange(list2);
-------------------You can simply add the items from one list to the other:
list1.AddRange(list2);
If you want to keep the lists and create a new one:
List<T> combined = new List<T>(list1);
combined.AddRange(list2);
Or using LINQ methods:
List<T> combined = list1.Concat(list2).ToList();
You can get a bit better performance by creating a list with the correct capacity before adding the items to it:
List<T> combined = new List<T>(list1.Count + list2.Count);
combined.AddRange(list1);
combined.AddRange(list2);
-------------------C # 3.0 / .Net 3.5를 사용하는 경우 :
List<SomeType> list1;
List<SomeType> list2;
var list = list1.Concat(list2).ToList();
-------------------.AddRange () 메서드 사용
http://msdn.microsoft.com/en-us/library/z883w3dc.aspx
출처
https://stackoverflow.com/questions/2002784