fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace TestApp
  6. {
  7.  
  8.  
  9. class Program
  10. {
  11.  
  12. private static List<User> users;
  13. private static List<Profile> profiles;
  14. private static List<Customer> customers;
  15.  
  16. static Program()
  17. {
  18. users = new List<User>(){new User{ID = 1}, new User(){ID=2}, new User(){ID=3}, new User(){ID=4}};
  19. customers = new List<Customer> {new Customer() {ID = 1}, new Customer() {ID = 2}, new Customer() {ID = 3}, new Customer(){ID=4}};
  20. profiles = new List<Profile>{new Profile(){ID= 1}, new Profile(){ID=2}, new Profile(){ID=3}};
  21.  
  22. users[0].Profiles.Add(profiles[0]);
  23. users[1].Profiles.Add(profiles[0]);
  24. users[2].Profiles.Add(profiles[0]);
  25. users[2].Profiles.Add(profiles[1]);
  26. users[3].Profiles.Add(profiles[2]);
  27.  
  28. profiles[0].Customers.Add(customers[0]);
  29. profiles[1].Customers.Add(customers[0]);
  30. profiles[1].Customers.Add(customers[1]);
  31. profiles[2].Customers.Add(customers[2]);
  32.  
  33. profiles[0].Users.Add(users[0]);
  34. profiles[0].Users.Add(users[1]);
  35. profiles[0].Users.Add(users[2]);
  36. profiles[1].Users.Add(users[2]);
  37. profiles[2].Users.Add(users[3]);
  38.  
  39. customers[0].Profiles.Add(profiles[0]);
  40. customers[0].Profiles.Add(profiles[1]);
  41. customers[1].Profiles.Add(profiles[1]);
  42. customers[2].Profiles.Add(profiles[2]);
  43. }
  44.  
  45. static void Main(string[] args)
  46. {
  47. var result = FindAllUsersBySameCustomers(users[0]);
  48. foreach (var user in result)
  49. {
  50. System.Console.WriteLine(user.ID);
  51. }
  52. System.Console.ReadLine();
  53. }
  54.  
  55. public static List<User> FindAllUsersBySameCustomers(User sourceuser)
  56. {
  57. var res = sourceuser.Profiles.SelectMany(p => p.Customers)
  58. .SelectMany(c => c.Profiles)
  59. .SelectMany(p => p.Users)
  60. .Distinct();
  61. return res.ToList();
  62. }
  63.  
  64. public class User
  65. {
  66. public User()
  67. {
  68. this.Profiles = new List<Profile>();
  69. }
  70.  
  71. public int ID { get; set; }
  72. // public bool IsValid { get; set; }
  73. public ICollection<Profile> Profiles { get; set; }
  74. }
  75.  
  76. public class Profile
  77. {
  78. public Profile()
  79. {
  80. this.Users = new List<User>();
  81. this.Customers = new List<Customer>();
  82. }
  83.  
  84. public int ID { get; set; }
  85. // public string Name { get; set; }
  86. public ICollection<User> Users { get; set; }
  87. public ICollection<Customer> Customers { get; set; }
  88. }
  89.  
  90. public class Customer
  91. {
  92. public Customer()
  93. {
  94. this.Profiles = new List<Profile>();
  95. }
  96.  
  97. public int ID { get; set; }
  98. // public string Number { get; set; }
  99. public ICollection<Profile> Profiles { get; set; }
  100. }
  101. }
  102. }
Success #stdin #stdout 0.04s 34112KB
stdin
Standard input is empty
stdout
1
2
3