简体   繁体   English

使用LINQ选择每个部门中收入最高的员工

[英]Using LINQ to select highest earning employee per department

Not really sure how to word it but basically I have a bunch of data in a list that goes similar to this: 不太确定该如何措词,但基本上我在列表中有很多数据,类似于以下内容:

Person 1 人1

Name - Joe Bloggs 姓名-Joe Bloggs
Age - 40 年龄-40
Department - IT 部门-IT
Wage - 20,000 工资-20,000

Person 2 人2

Name - Jess Jane 名字-Jess Jane
Age - 40 年龄-40
Department - Kitchen 部门-厨房
Wage - 16,000 工资-16,000

...you get the idea. ...你明白了。

At the moment, I've just selected all of the people, ordered them by wage and entered them in a listbox very simply by doing this. 此刻,我刚刚选择了所有人员,按工资顺序对其进行排序,并通过此操作非常简单地将其输入到列表框中。

var item = (from employee in employeeList.employees
orderby employee.wage descending
select employee);

Now, my question is, how can I change this bit of code so that it filters through the list and show only the highest earning employee in their department? 现在,我的问题是,如何更改这段代码,以便对列表进行过滤并仅显示其部门中收入最高的员工? So for example, instead of having hundreds of employees listed, it will only show the highest earning employee in IT, then the highest earning employee in catering, etc. 因此,例如,它没有列出数百名员工,而是仅显示IT中收入最高的员工,然后显示餐饮中收入最高的员工,等等。

Is it possible? 可能吗? If not are there any other methods I can use to achieve this? 如果没有,我可以使用任何其他方法来实现这一目标吗?

You could try something like this: 您可以尝试这样的事情:

var result =  employeeList.employees.GroupBy(emp=>emp.Departemnt)
                                    .Select(gr=> new 
                                    { 
                                        Departemnt = gr.Key, 
                                        Employee = gr.OrderByDescending(x=>x.wage)
                                                     .FirstOrDefault() 
                                    });

That we do above is a grouping by department and the we pick for each department the employee with the highest wage. 我们在上面所做的是按部门分组,我们为每个部门选择工资最高的员工。

Something along these line : 这些东西:

var highSalemployeePerDept= employeeList.employees.GroupBy(x => x.Department)
                 .Select(g => g.OrderByDescending(x => x.Salary).First());
var highestEarnersByDept =
            employeeList.employees.GroupBy(employee => employee.Department)
                .Select(grouping => grouping.OrderByDescending(employee => employee.Wage).First())
                .ToArray();

This approach lets you get the result without ordering. 这种方法使您无需排序即可获得结果。 So it will take only O(n) time instead of O(n*log(n)) 因此只需要O(n)时间,而不是O(n * log(n))

var highestEarningPersonByDepartment = persons
    .GroupBy(p => p.Department)
    .Select(g => new { Department = g.Key, HighestEarningPerson = g.First(person => person.Wage == g.Max(p => p.Wage)) })
    .ToDictionary(dp => dp.Department, dp => dp.HighestEarningPerson);

Edit: A more optimised version would be: 编辑:一个更优化的版本将是:

var maxWageByDepartment = persons
    .GroupBy(p => p.Department)
    .ToDictionary(g => g.Key, g => g.Max(p => p.Wage));

var richestPersonByDepartment = persons
    .GroupBy(p => p.Department)
    .Select(g => new { Department = g.Key, HighestEarningPerson = g.First(person => person.Wage == maxWageByDepartment[g.Key]) });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM