I'm trying to implement a method to concatenate multiple List
s e.g.
List<string> l1 = new List<string> { "1", "2" }; List<string> l2 = new List<string> { "1", "2" }; List<string> l3 = new List<string> { "1", "2" }; var result = Concatenate(l1, l2, l3);
but my method doesn't work:
public static IEnumerable<T> Concatenate<T>(params IEnumerable<T> List) { var temp = List.First(); for (int i = 1; i < List.Count(); i++) { temp = Enumerable.Concat(temp, List.ElementAt(i)); } return temp; }
Use SelectMany
:
public static IEnumerable<T> Concatenate<T>(params IEnumerable<T>[] lists) { return lists.SelectMany(x => x); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With