The easiest way is to possibly βsmooth outβ the problem and then make the group:
var query = officers.SelectMany( x => new[] { new { x.Name, RO = x.ReportingOfficer1 }, new { x.Name, RO = x.ReportingOfficer2 }, new { x.Name, RO = x.ReportingOfficer3 } } ); var grouped = query.GroupBy(y => y.RO); foreach (var group in grouped) { foreach (var item in group) { Console.WriteLine(String.Format("{0}: {1}", item.RO, item.Name)); } }
Here I assume that officers are IEnumerable<Officer> , where
class Officer { public string Name { get; set; } public string ReportingOfficer1 { get; set; } public string ReportingOfficer2 { get; set; } public string ReportingOfficer3 { get; set; } }
With your sample data, this is my conclusion:
John: A John: B Jack: A Rob: A Rob: C Earl: B Carl: B Carl: D Chris: C Kenny: C Rodney: D Jacob: D
source share