Linq离开了加入和统计 - 如何

时间:2014-09-13 20:33:24

标签: c# linq linq-to-entities

我有以下Linq表达式:

var employeeTypes = from t in DbContext.Set<SetupEmployeeType>().AsNoTracking()
                    join emp in DbContext.Set<Employee>().AsNoTracking() on t.EmployeeTypeId equals emp.EmployeeTypeId into employee
                    from subemp in employee.DefaultIfEmpty()
                    where t.MasterEntity == masterEntity
                    select new Model.SetupEmployeeTypeModel()
                    {
                        EmployeeTypeId = t.EmployeeTypeId,
                        Description = t.Description,
                        AllowProbation = t.AllowProbation,
                        IsActive = t.IsActive,
                        TotalEmployee = (subemp == null ? 0 : subemp.Count) 

                    };

我需要设置自定义模型的TotalEmployee属性。

因此,如果没有EmployeeTypeId与任何员工关联,则TotalEmployee 0 ,否则应为员工人数。< / p>

有任何线索如何做到这一点?

2 个答案:

答案 0 :(得分:0)

如果我没有错,那么考虑使用子查询,如此 -

var q = from empType in DbContext.Set<SetupEmployeeType>().AsNoTracking()
        let empCount =
        (
          from emp in DbContext.Set<Employee>().AsNoTracking()
          where empType.EmployeeTypeId == emp.EmployeeTypeId
          select emp
        ).Count()
        select new Model.SetupEmployeeTypeModel()
        {
            EmployeeTypeId = empType.EmployeeTypeId,
            Description = empType.Description,
            AllowProbation = empType.AllowProbation,
            IsActive = empType.IsActive,
            TotalEmployee = empCount
        };

使用分组依据。

var q = from empType in DbContext.Set<SetupEmployeeType>().AsNoTracking()
        join empCnt in 
            (
                from emp in DbContext.Set<Employee>().AsNoTracking()
                group emp by emp.EmployeeTypeId into grp
                select new { EmployeeTypeId = grp.Key, TotalEmp = grp.Count()}
            ) on empType.EmployeeTypeId equals empCnt.EmployeeTypeId into employees
        from subemp in employees.DefaultIfEmpty()
        where t.MasterEntity == masterEntity
        select new Model.SetupEmployeeTypeModel()
        {
            EmployeeTypeId = empType.EmployeeTypeId,
            Description = empType.Description,
            AllowProbation = empType.AllowProbation,
            IsActive = empType.IsActive,
            TotalEmployee = subemp.TotalEmp
        };

答案 1 :(得分:0)

我提出了以下解决方案:

 var employeeTypes = from t in DbContext.Set<SetupEmployeeType>().AsNoTracking()
                                join empg in
                                    (
                                        from emp in DbContext.Set<Employee>().AsNoTracking()
                                        group emp by emp.EmployeeTypeId into g
                                        select new { EmployeeTypeId = g.Key, Total = g.Count() }
                                    ) on t.EmployeeTypeId equals empg.EmployeeTypeId into employee
                                from subemp in employee.DefaultIfEmpty()
                                where t.MasterEntity == masterEntity

                                select new Model.SetupEmployeeTypeModel()
                                {
                                    EmployeeTypeId = t.EmployeeTypeId,
                                    Description = t.Description,
                                    AllowProbation = t.AllowProbation,
                                    IsActive = t.IsActive,
                                    TotalEmployee = subemp.Total 

                                };