zoukankan      html  css  js  c++  java
  • .net EF Join 关联表分页查询

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

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

                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();


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

  • 相关阅读:
    如何优化数据库中数据的查询
    2句sql语句的比较
    未来五年程序员应当具备的十项技能
    中小IT企业如何留住80后技术人才
    ORACLE的sign函数和DECODE函数
    Windows Server 2008系统 安装Oracle 10g 错误
    ORACLE 错误 ora01830 解决方法
    oracle instr函数
    Oracle 9i安装时没有找到OCS4J.properties 解决办法
    C# 中的委托和事件
  • 原文地址:https://www.cnblogs.com/asd14828/p/6514321.html
Copyright © 2011-2022 走看看