简体   繁体   English

在实体框架的每个父属性中获得最佳结果

[英]Get top result in each parent property in Entity Framework

I'm trying to select the employees with the most reports in each department. 我试图选择每个部门中报告最多的员工。 I can't figure out how to do this though with LINQ in Entity Framework. 我无法弄清楚如何使用Entity Framework中的LINQ做到这一点。 I'm using EF Core, but the answer should be the same as EF 6 so an answer for either will work. 我使用的是EF Core,但答案应与EF 6相同,因此任一答案都可以。 I know I'll need to use .Distinct() , but I'm not sure how to use it properly. 我知道我需要使用.Distinct() ,但是我不确定如何正确使用它。

How can I get the employee in each department with the most reports? 如何获得每个部门中报告最多的员工? The query should return a list (or queryable) of employees. 该查询应返回员工列表(或可查询)。

public class Employee
{
    public long Id { get; set; }
    public string Name { get; set; }

    public long DepartmentId { get; set; }
    public Department Department { get; set; }

    public ICollection<Report> Reports { get; set; }
}

public class Department
{
    public long Id { get; set; }
    public string Name { get; set; }

    public ICollection<Employee> Employees { get; set; }
}

This is what I started with, but I'm not sure if it's even the right way to begin. 这就是我的开始,但是我不确定这是否是正确的开始方式。

var employees = _context.Employees
    .OrderByDescending(e => e.Reports.Count)
    // ?

Just take from each department the employee with the most reports: 只需从每个部门中获取报告最多的员工即可:

var employees = _context.Departments.Select(department => 
                  department.Employees.OrderByDescending(employee => employee.Reports.Count).FirstOrDefault()).ToList();

You should start the query at the department level. 您应该在部门级别开始查询。 Within each department, get its employee with the highest report count: 在每个部门内,让其员工拥有最高的报告数量:

var topEmpPerDep = _context.Departments
    .Select(dep => new
    {
        Department = dep.Name,
        Employee = dep.Employees.OrderByDescending(e => e.Reports.Count)
           .FirstOrDefault().Name
    });

Try this : 尝试这个 :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var results = Department.departments.Select(x => new {
                deptmentName = x.Name,
                maxEmployeeReports = x.Employees.Select(y => new { name = y.Name, count = y.Reports.Count, employee = y }).OrderByDescending(z => z.count).FirstOrDefault()
            }).ToList();
        }
    }
    public class Employee
    {
        public long Id { get; set; }
        public string Name { get; set; }

        public long DepartmentId { get; set; }
        public Department Department { get; set; }

        public ICollection<Report> Reports { get; set; }
    }

    public class Department
    {
        public long Id { get; set; }
        public string Name { get; set; }

        public ICollection<Employee> Employees { get; set; }
        public static List<Department> departments = new List<Department>();
    }
    public class Report
    {
    }
}

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

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