using System; using System.Collections; using System.Collections.Generic; class Program { static void Main(string[] args) { //Test with classes string[] classesList1 = new[] {"a", "b", "c"}; string[] classesList2 = new[] { "1", "2", "3" }; List classesListCombined = new List { classesList1, classesList2 }; Console.WriteLine("classesList1: {0}", String.Join(",", classesList1.SelectManyRecusive())); Console.WriteLine("classesListCombined: {0}", String.Join(",", classesListCombined.SelectManyRecusive())); //Test with structs int[] structList1 = new[] { 1, 2, 3 }; int[] structList2 = new[] { 4, 5, 6 }; List structListCombined = new List { structList1, structList2 }; Console.WriteLine("structList1: {0}", String.Join(",", structList1.SelectManyRecusive())); Console.WriteLine("structListCombined: {0}", String.Join(",", structListCombined.SelectManyRecusive())); //Test tripple nesting var nest1 = new[] {1, 2, 3}; var nest2 = new[] { 4, 5, 6 }; var nest3 = new[] { 7, 8, 9 }; var nest4 = new[] {10, 11, 12}; var layer1 = new[] {nest1, nest2}; var layer2 = new[] {nest3, nest4}; var top = new[] {layer1, layer2}; Console.WriteLine("top: {0}", String.Join(",", top.SelectManyRecusive())); Console.ReadLine(); } } internal static class ExtensionMethods { public static IEnumerable SelectManyRecusive(this IEnumerable enumerable) { foreach (var item in enumerable) { var castEnumerable = item as IEnumerable; if (castEnumerable != null && ((typeof(T) != typeof(string)) || !(castEnumerable is string))) //Don't split string to char if string is our target { foreach (var inner in SelectManyRecusive(castEnumerable)) { yield return inner; } } else { if (item is T) { yield return (T) item; } } } } }