EF架构~为导航属性赋值时ToList()的替换方案

回到目录

今天在进行EF开发时,遇到一个问题,在进行join查询时,类中的一个集合类型的导航属性,在给它赋值时,将查询出来的结果ToList()后,出错了,linq to entity不支持这种操作,而在linq to sql里这是合法的,在EF中是不行的,所以,使用了替换方法,就是类型强转,代码如下:

 var linq = from student in base.GetModel()
                       join user_classrooms in new TsingDa_NewLearningBarRepository<User_Classroom_R>(base.UnitWork).GetModel()
                       on student.UserInfoID equals user_classrooms.UserInfoID
                       join classroom_Info in new TsingDa_NewLearningBarRepository<Classroom_Info>(base.UnitWork).GetModel()
                       on user_classrooms.ClassroomInfoID equals classroom_Info.ClassroomInfoID
                       join teacher in base.GetModel()
                       on classroom_Info.UserInfoID equals teacher.UserInfoID
                       join classroom_Plan in new TsingDa_NewLearningBarRepository<Classroom_Plan>(base.UnitWork).GetModel()
                       on user_classrooms.ClassroomInfoID equals classroom_Plan.ClassroomInfoID into list
                       join classroom_CommonValue_R in new TsingDa_NewLearningBarRepository<Classroom_CommonValue_R>(base.UnitWork).GetModel()
                       on user_classrooms.ClassroomInfoID equals classroom_CommonValue_R.ClassroomInfoID into valueList
                       join user_ClassroomPlan_Study in new TsingDa_NewLearningBarRepository<User_ClassroomPlan_Study>(base.UnitWork).GetModel()
                       .Where(i => i.ClassroomPlanID == 0 && i.StudyStatus == (int)StudyStatus.Study)

                       on new user_classroom
                       {
                           UserInfoID = student.UserInfoID,
                           ClassroomInfoID = user_classrooms.ClassroomInfoID
                       } equals new user_classroom
                       {
                           UserInfoID = user_ClassroomPlan_Study.UserInfoID,
                           ClassroomInfoID = user_ClassroomPlan_Study.ClassroomInfoID
                       } into userplane
                       let status = userplane.Count() == 0 ? (int)StudyStatus.Study : (int)StudyStatus.Finish
                       let scale = (list.Count() == 0) ? 0 : userplane.Count() - 1 / list.Count()
                       select new Classroom_Info_Ext
                       {
                           ClassroomInfoID = classroom_Info.ClassroomInfoID,
                           Title = classroom_Info.Title,
                           Type = classroom_Info.Type,
                           EndTime = user_classrooms.EndTime,
                           Student_Info = student,
                           Teacher_Info = teacher,
                           StudyScale = scale,
                           StudyStatus = (StudyStatus)status,
                           Classroom_CommonValue_R = (List<Classroom_CommonValue_R>)valueList //这里这前是valueList.ToList()
};

而在分页返回时,也使用了类型转换,这种转换是内存中自动完成,所以性能方面不用考虑

    var result = linq.AsQueryable<Classroom_Info>().Where(spec.SatisfiedBy()).OrderBy(i => i.ClassroomInfoID);
    return new PagedList<Classroom_Info>(result, pp.PageIndex, pp.PageSize);

对于linq to entity有很多不便的地方,如在使用skip之前,必须要对iqueryable进行ordery操作;在实体赋值时不能使用逻辑判断等等

回到目录

你可能感兴趣的:(list)