fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. public class Test
  5. {
  6.  
  7. public static void Main(){
  8. var personTables = new List<Persontable>{
  9. new Persontable{Name = "John", AllApartments = new List<int>{1, 2, 3}, Active = false},
  10. new Persontable{Name = "Mary", AllApartments = new List<int>{1}, Active = true},
  11. };
  12.  
  13. var apartments = new List<Apartment>{
  14. new Apartment { Id = 1, Name = "LA downtown apts"},
  15. new Apartment { Id = 2, Name = "NYC downtown apts"},
  16. };
  17.  
  18. var result = personTables.Select( p => new PersontableAparment{
  19. Name = p.Name,
  20. AllApartmentsNames = apartments.Where(a => p.AllApartments.Contains(a.Id)).Select(x => x.Name).ToList(),
  21. Active = p.Active,
  22. });
  23.  
  24. foreach (var item in result){
  25. Console.WriteLine("Name: " + item.Name);
  26. foreach(var apartment in item.AllApartmentsNames)
  27. {
  28. Console.WriteLine("Apartment Name:" + apartment);
  29.  
  30. }
  31.  
  32. Console.WriteLine("Active: " + item.Active);
  33. }
  34. }
  35.  
  36. public class Persontable
  37. {
  38. public string Name { get; set;}
  39. public List<int> AllApartments { get; set;}
  40. public bool Active {get; set;}
  41. }
  42.  
  43. public class Apartment
  44. {
  45. public int Id {get; set;}
  46. public string Name {get; set;}
  47. }
  48.  
  49. public class PersontableAparment
  50. {
  51. public string Name { get; set; }
  52. public List<string> AllApartmentsNames { get; set; }
  53. public bool Active { get; set; }
  54.  
  55. }
  56. }
Success #stdin #stdout 0.06s 24104KB
stdin
Standard input is empty
stdout
Name: John
Apartment Name:LA downtown apts
Apartment Name:NYC downtown apts
Active: False
Name: Mary
Apartment Name:LA downtown apts
Active: True