本文介绍了集团通过使用C#LINQ或者列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组数据的列,如下面

I have a set of Data with columns such as below

OffName,RO1,RO2,RO3

要进一步我使用的样本数据如下解释:

To explain further i use sample data as below:

OffName RO1    RO2   RO3 
A       John   Jack  Rob 
B       Earl   John  Carl 
C       Rob    Chris Kenny 
D       Rodney Carl  Jacob 

RO代表报告员。以高达3 RO's.i每名人员的报告需要做一个报告,我需要表现由RO分组无关的人是RO1或RO2和RO3为officer..John是RO1的官员A和RO2的官员B,因此通过RO在约翰分组的时候,我想官员急症室两个; B至是picked.Same卡尔是RO3的官员B和RO2的干事D因此通过滚装卡尔下,当分组官B和两个; ð被拾起。

RO stands for Reporting Officer. Each Officer reports to upto 3 RO's.i need to make a report where i need to show a grouping by RO irrespective of the person is RO1 or RO2 or RO3 for the officer..John is RO1 for Officer A and RO2 for Officer B, so when grouped by RO under John i want both Officer A & B to be picked.Same for Carl is RO3 for Officer B and RO2 for Officer D so when grouped by Ro under Carl both Officer B & D to be picked..

因此​​,对于上述数据时,由RO的分组我希望结果是一样

So for the above data when grouped by RO's i want the result to be as

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

任何帮助将b极

Any help would b great

感谢。

推荐答案

最简单的方法可能是的问题,然后做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

这篇关于集团通过使用C#LINQ或者列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 19:34