.net EF Join 关联表分页查询

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/duanwenzhi00/article/details/52583465

Join中第二个参数和第三个参数即为两张表关联对应的字段

第四个参数new 后面如果不写Model名,系统会自动生成带字段的list类型

public List<OperatorTrainingReportVM> GetOperatorTrainingReportByStart(int start, int pageSize, out int total, Expression<Func<OperatorTrainingReportVM, bool>> whLamdba)

        {
            IQueryable<Operator> operators = operatorService.GetAll();
            IQueryable<Training> trainings = trainingService.GetAll();
            var list = trainings.Join(operators, a => a.OperatorId, b => b.Id, (a, b) => new OperatorTrainingReportVM
            {
                EmployeeNO = b.EmployeeNO,
                Name = b.Name,
                Sex = b.Sex,
                AreaName = b.Area.Name,
                CostName = b.Cost.Name,
                OnBoardDate = b.OnBoardDate,
                ShiftName = b.Shift.Name,
                DirectorNO = b.Director.EmployeeNO,
                DirectorName = b.Director.Name,
                ConfirmDate = b.ConfirmDate,
                IsDimission = b.DimissionDate != null ? true : false,
                DimissionDate = b.DimissionDate,
                ServiceDays = (int)DbFunctions.DiffDays(b.OnBoardDate, DateTime.Now),
                PositionName = b.Position.Name,
                PositionLevelName = b.PositionLevel.Name,
                DimissionName = b.Dimission.Name,
                DimissionReason = b.Dimission.Reason,
                StartDate = a.StartDate,
                EndDate = a.EndDate,
                ExamineDate = a.ExamineDate,
                ExpireDate = a.ExpireDate,
                Enginner = a.Enginner,
                Trainer = a.Trainer,
                Score = a.Score,
                StandardScore = a.StandardScore,
                Result = a.Result,
                CourseName = a.Course.Name,
                OperationLevelName = a.OperationLevel.Name,
                Enabled = b.Enabled
            }).Where(whLamdba);
            total = list.Count();
            var result = list.OrderBy(t => t.EmployeeNO).Skip(start).Take(pageSize).ToList();

            return result;

        }


其中Join部分具体是inner join还是left join,EF会自动根据查询条件生成相应的SQL


开发中刚学习到,如果不正确的地方,欢迎指正

展开阅读全文

没有更多推荐了,返回首页