【问题标题】:Linq get id of the employee with most entries in tableLinq获取表中条目最多的员工的ID
【发布时间】:2021-07-08 13:01:45
【问题描述】:

我正在尝试编写一个 linq,它将返回表中条目最多的员工的 id。

这就是我的班级的样子

public class TrainingEmployee
{
    public int EmployeeId { get; set; }
    public int TrainingId { get; set; }

    public List<TrainingEmployee> GenerateData()
    {
        return new List<TrainingEmployee>()
        {
            new TrainingEmployee() { EmployeeId = 1, TrainingId = 2},
            new TrainingEmployee() { EmployeeId = 1, TrainingId = 2},
            new TrainingEmployee() { EmployeeId = 1, TrainingId = 2},
            new TrainingEmployee() { EmployeeId = 1, TrainingId = 2},
            new TrainingEmployee() { EmployeeId = 2, TrainingId = 3},
            new TrainingEmployee() { EmployeeId = 2, TrainingId = 3},
            new TrainingEmployee() { EmployeeId = 2, TrainingId = 3},
            new TrainingEmployee() { EmployeeId = 2, TrainingId = 3},
            new TrainingEmployee() { EmployeeId = 2, TrainingId = 5},
            new TrainingEmployee() { EmployeeId = 2, TrainingId = 5},
            new TrainingEmployee() { EmployeeId = 2, TrainingId = 1},

        };
    }
}

这就是我的代码到目前为止的样子

var lista = new TrainingEmployee();

        var data = lista.GenerateData().GroupBy(x => x.EmployeeId);
        var maxValue = 0;
        var employeeId = 0;
        foreach (var group in data)
        {
            
            var currentlyGroupCount = group.Count();
            if(currentlyGroupCount > maxValue)
            {
                maxValue = currentlyGroupCount;
                employeeId = group.Key;
            }
        }
        Console.WriteLine("Value: {0} employeeid: {1}", maxValue, employeeId);
       

如何在不使用那么多代码的情况下仅在 linq 中完成上述代码?

【问题讨论】:

    标签: c# .net linq


    【解决方案1】:

    您可以按降序排列并选择第一个:

    var employee = GenerateData()
        // group on EmployeeId
        .GroupBy(e => e.EmployeeId)
        // reverse order it on count
        .OrderByDescending(g => g.Count())
        // select the first
        .FirstOrDefault();
    
    // check if the query returned anything other than default.
    if(employee != default)
        Console.WriteLine("Value: {0} employeeid: {1}", employee.Count(), employee.EmployeeId);
    

    【讨论】:

      【解决方案2】:

      另一种方法类似于jeroen-van-langen 的答案,但使用的是MoreLINQ 的MaxBy()

      GenerateData()
        .GroupBy(e => e.EmployeeId)
        .MaxBy(e => e.Count());
      

      如果多个员工的“最大计数”相同,这也会返回多个 ID;您的场景中的一种可能性。

      【讨论】:

        【解决方案3】:

        这会为每个员工组评估一次 Count(),因此性能更高一些,它还允许同时获取employyId 和最大计数

            var mostFrequentEmployeeId = GenerateData()
                .GroupBy(x => x.EmployeeId, (employeeId, employeesGroup) => new { employeeId, count = employeesGroup.Count() })
                .OrderByDescending(x => x.count)
                .FirstOrDefault()?
                .employeeId;
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-06
          • 2021-12-09
          • 1970-01-01
          • 2022-12-12
          相关资源
          最近更新 更多