且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

集团通过使用C#LINQ或者列

更新时间:2023-01-17 16:12:55

最简单的方法可能是的扁平化问题,然后做GROUP BY:

The easiest way is probably to "flatten" the problem and then do the group by:

    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));
        }
    }

在这里,我假设人员的IEnumerable<官> 其中

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 output:

John: A
John: B
Jack: A
Rob: A
Rob: C
Earl: B
Carl: B
Carl: D
Chris: C
Kenny: C
Rodney: D
Jacob: D