using System.Collections.Generic;
public static void Main()
var groups = Person.MergePerson(Person.r);
foreach (var group in groups)
Console.WriteLine("Группа: " + group.Key);
foreach (Person person in group.Value)
Console.WriteLine(@"Person {0} {1} {2}", person.a, person.b, person.ndx);
public Person(string a, string b, int ndx)
public static List<Person> r = new List<Person> {
new Person("10", "1", 0),
new Person("10", "2", 1),
new Person("11", "3", 2),
new Person("11", "4", 3),
new Person("10", "4", 4),
public static Dictionary<int, List<Person>> MergePerson(List<Person> r)
var dic = new Dictionary<int, List<Person>>();
foreach (var person in r)
foreach (var list in dic.Values.Where(list => !list.Contains(person)).Where(list => list.Any(per => per.a == person.a || per.b == person.b)))
if (!dic.Values.Any(list => list.Contains(person)))
dic.Add(numGroup++, r.Where(personList => (personList.a == person.a) || (personList.b == person.b)).Select(z => z).ToList());