简体   繁体   English

使用LINQ - Entity Framework获取其他字段的最快方法是什么

[英]What is fastest way to get count with other fields using LINQ - Entity Framework

I am fetching employee count for each result: 我为每个结果获取员工数:

var query = (from Department dept in database.Department
             join emp in database.Employees on dept.EmployeeId equals emp.EmployeeId
             where dept.IOfficeId == officeId 
             select new EmployeeData
                    {
                        FullName = emp.Name,
                        EmployeeId = dept.EmployeeId,
                        DepartmentName = dept.Name,
                        EmployeeCount = 
                             (from Employees emp_count in database.Employees 
                              where emp_count.DepartmentId == dept.DepartmentId).Count()
                    });

Please suggest faster and more efficient method to fetch the data. 请建议更快,更有效的方法来获取数据。

Since the employee-count-per-department is a fairly simple query, which would also be redundant if you execute it for every employee, I would first query the employee-count-per-department, and use that result afterwards in your current query. 由于每个部门的employee-count是一个相当简单的查询,如果你为每个员工执行它,这也是多余的,我首先会查询每个部门的employee-count,然后在当前查询中使用该结果。

var departmentEmployeeCount = database.Employees
    .GroupBy(x => x.DepartmentId)
    .Select(x => new { DepartmentId = x.Key, Count = x.Count() })
    .ToArray();

var query = (from Department dept in database.Department
             join emp in database.Employees on dept.EmployeeId equals emp.EmployeeId
             where dept.IOfficeId == officeId
             select new {
                emp.Name,
                dept.EmployeeId,
                dept.Name,
                dept.DepartmentId
             }
            )
            .AsEnumerable()
            .Select(x => new EmployeeData {
                FullName = x.Name,
                EmployeeId = x.EmployeeId,
                DepartmentName = x.Name,
                EmployeeCount = departmentEmployeeCount
                    .Where(z => z.DepartmentId == x.DepartmentId)
                    .Select(x => x.Count)
                    .FirstOrDefault()
                }
            );

I must say that I find it confusing that your Employees table contains a column DepartmentId and Department contains a column EmployeeId . 我必须说,我发现令人困惑的是,您的Employees表包含一个DepartmentId列, Department包含一列EmployeeId Seems like two foreign keys connecting the same table. 好像连接同一个表的两个外键。

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

相关问题 使用LINQ和Entity Framework获取延迟加载的子级集合的计数 - Get Count of Lazy Loaded Child Collection Using LINQ and Entity Framework 在实体框架中插入的最快方法 - Fastest Way of Inserting in Entity Framework 实体框架排除字段查询计数和POCO最佳方法 - Entity Framework Exclude Fields Query Count and POCO best way 使用Linq to SQL确定行是否存在的最快方法是什么? - What is the fastest way to determine if a row exists using Linq to SQL? 从实体对象获取ObjectContext引用的最快方法是什么? - What's the fastest way to get an ObjectContext reference from an entity object? 实体框架错误,试图获取Linq到实体的行数 - Entity Framework Error Trying to Get Row Count in Linq to Entities 使用 linq 查询语法连接两个列表的最快方法是什么? - What is the fastest way to join two lists using linq query syntax? 使用实体框架和LINQ到实体时分配外键的最佳方法是什么? - What is the best way in assigning foreign key when using entity framework & LINQ to Entities? 实体框架核心:检查通用实体是否已存在于dbset中然后添加或更新的最快方法是什么? - Entity Framework Core: What is the fastest way to check if a generic entity is already in the dbset and then add or update it? 在LINQ to Entities中检查重复项的最快方法是什么? - What is the fastest way to check for duplicates in LINQ to Entities?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM