fork(1) download
  1. using System;
  2. using System.Linq;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5.  
  6. public class Test
  7. {
  8. public static void Main()
  9. {
  10. var myList =
  11. new List<ObjectA>{
  12. new ObjectA{
  13. Name = "ItemA 1",
  14. Children = new List<ObjectB>{
  15. new ObjectB{ChildName = "ItemB 1"},
  16. new ObjectB{ChildName = "ItemB 2"}
  17. }
  18. },
  19. new ObjectA{
  20. Name = "ItemA 2",
  21. Children = new List<ObjectB>{
  22. new ObjectB{ChildName = "ItemB 3"},
  23. new ObjectB{ChildName = "ItemB 4"}
  24. }
  25. }
  26. };
  27. // What code would I put here to concat all the ObjectBs?
  28. var allObjB = new List<ObjectB>();
  29. myList.ForEach(x => allObjB.AddRange(x.Children));
  30. allObjB.ForEach(x => Console.WriteLine(x.ChildName));
  31. }
  32. }
  33. public class ObjectA
  34. {
  35. public string Name {get; set;}
  36. public List<ObjectB> Children {get; set;}
  37. }
  38.  
  39. public class ObjectB
  40. {
  41. public string ChildName {get; set;}
  42. }
Success #stdin #stdout 0.01s 29672KB
stdin
Standard input is empty
stdout
ItemB 1
ItemB 2
ItemB 3
ItemB 4