So you have combinations of [ColumnA, ColumnB], and if you want all values of ColumnA where there is more than one ColumnB.
My advice would be to convert your [ColumnA, ColumnB] into groups of "ColumnB, with all ColumnA that have this ColumnB. Then keep only the Groups that have more than one ColumnA.
So in your example:
- 1 with values A, B
- 2 with values C, D
- 3 with value E
Throw away group 3 because it has only one element, and flatten the result.
We use the overload of GroupBy that has an elementSelector. The elementSelector will select columnA as elements.
var combinationsColumnAColumnB = ...
var result = combinationsColumnB.GroupBy(
// parameter keySelector: make groups with same ColumnB
combination => combination.ColumnB,
// parameter elementselector: put only the ColumnA in each group
combination => combination.ColumnA)
// result: groups of columnB with all columnA that belong to this columnB
// keep only those groups that have more than one element
.Where(group => group.Skip(1).Any()
// and flatten the result:
.SelectMany(group => group);
If you have combinations like:
A 1
A 2
B 1
C 2
You will get element A twice. If you don't want that, add a Distinct()