using System;
using System.Collections.Generic;
using System.Linq;
namespace TestApp
{
class Program
{
private static List<User> users;
private static List<Profile> profiles;
private static List<Customer> customers;
static Program()
{
users = new List<User>(){new User{ID = 1}, new User(){ID=2}, new User(){ID=3}, new User(){ID=4}};
customers = new List<Customer> {new Customer() {ID = 1}, new Customer() {ID = 2}, new Customer() {ID = 3}, new Customer(){ID=4}};
profiles = new List<Profile>{new Profile(){ID= 1}, new Profile(){ID=2}, new Profile(){ID=3}};
users[0].Profiles.Add(profiles[0]);
users[1].Profiles.Add(profiles[0]);
users[2].Profiles.Add(profiles[0]);
users[2].Profiles.Add(profiles[1]);
users[3].Profiles.Add(profiles[2]);
profiles[0].Customers.Add(customers[0]);
profiles[1].Customers.Add(customers[0]);
profiles[1].Customers.Add(customers[1]);
profiles[2].Customers.Add(customers[2]);
profiles[0].Users.Add(users[0]);
profiles[0].Users.Add(users[1]);
profiles[0].Users.Add(users[2]);
profiles[1].Users.Add(users[2]);
profiles[2].Users.Add(users[3]);
customers[0].Profiles.Add(profiles[0]);
customers[0].Profiles.Add(profiles[1]);
customers[1].Profiles.Add(profiles[1]);
customers[2].Profiles.Add(profiles[2]);
}
static void Main(string[] args)
{
var result = FindAllUsersBySameCustomers(users[0]);
foreach (var user in result)
{
System.Console.WriteLine(user.ID);
}
System.Console.ReadLine();
}
public static List<User> FindAllUsersBySameCustomers(User sourceuser)
{
var res = sourceuser.Profiles.SelectMany(p => p.Customers)
.SelectMany(c => c.Profiles)
.SelectMany(p => p.Users)
.Distinct();
return res.ToList();
}
public class User
{
public User()
{
this.Profiles = new List<Profile>();
}
public int ID { get; set; }
// public bool IsValid { get; set; }
public ICollection<Profile> Profiles { get; set; }
}
public class Profile
{
public Profile()
{
this.Users = new List<User>();
this.Customers = new List<Customer>();
}
public int ID { get; set; }
// public string Name { get; set; }
public ICollection<User> Users { get; set; }
public ICollection<Customer> Customers { get; set; }
}
public class Customer
{
public Customer()
{
this.Profiles = new List<Profile>();
}
public int ID { get; set; }
// public string Number { get; set; }
public ICollection<Profile> Profiles { get; set; }
}
}
}