// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Collections.Generic; using System.Linq; namespace E { public class DBCity { public string Name { get; set; } } public class DBCountry { public string Name { get; set; } public List Cities { get; set; } } public class City { public string Name { get; set; } } public class Country { public string Name { get; set; } public List Cities { get; set; } } public class Program { public static void Main(string[] args) { List dblst = new List { new DBCountry { Name = "Denmark", Cities = new List { new DBCity { Name = "Copenhagen" }, new DBCity { Name = "Aarhus" } } }, new DBCountry { Name = "Sweden", Cities = new List { new DBCity { Name = "Stockholm" }, new DBCity { Name = "Malmoe" } } } }; foreach(DBCountry country in dblst) { Console.WriteLine(country.Name); foreach(DBCity city in country.Cities) { Console.WriteLine(" " + city.Name); } } List lst = dblst.Select(c => new Country { Name = c.Name, Cities = c.Cities.Select(c2 => new City { Name = c2.Name }).ToList() }).ToList(); foreach(Country country in lst) { Console.WriteLine(country.Name); foreach(City city in country.Cities) { Console.WriteLine(" " + city.Name); } } } } }