码迷,mamicode.com
首页 > 其他好文 > 详细

EF应用带来的好处

时间:2015-01-29 22:35:26      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:优化   entity framework   

让大家看一下改版前的代码和我改版之后的代码。你就明白了。。

背景:有学生表,也有一张班级表。通过在学生表中加入外键班级ID。现在我需要查询到学生表的信息 并查询到相关的班级信息 同时保存在学生的数据契约中。

改版前:

代码块一:

  #region (考试接口)查询所有的在校学生+List<Student> QueryAllStudent()

        /// <summary>
        /// (考试接口)查询所有的在校学生 包括班级名称
        /// </summary>
        /// <returns></returns>
        public List<Student> QueryAllStudent()
        {
            List<BasicStudentEntity> result0 = (List<BasicStudentEntity>)studentInfoService.LoadPageItems(u => u.IsGraduated == 0 && u.IsEnabled == 1, u => u.StudentNo, true).ToList();
        
            //给学生添加班级信息
            List<Student> StudentandClassName = QueryStudentandClassName(StudnetandSchool);

            return StudentandClassName;
        }
        #endregion

上面代码中调用下面这个查询班级信息的方法。通过循环匹配实现

代码块二:


        #region 查询所有学生所在的班级信息 用在前台显示+List<Student> QueryStudentandClassName(List<BasicStudentEntity> result)
        /// <summary>
        /// 查询所有学生所在的班级信息 用在前台显示
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        public List<Student> QueryStudentandClassName(List<BasicStudentEntity> result)
        {
            List<BasicClassEntity> classresult = (List<BasicClassEntity>)classInfoService.LoadEnities(u=>u.IsEnabled==1).ToList();
            List<BasicOrganizationEntity> schoolresult = (List<BasicOrganizationEntity>)organizationInfoService.LoadEnities().ToList();

            for (int studentCount = 0; studentCount < result.Count(); studentCount++)
            {
                for (int classCount = 0; classCount < classresult.Count(); classCount++)
                {

                    if (result[studentCount].ClassID == classresult[classCount].ClassID)
                    {
                        result[studentCount].BasicClassEntity.ClassName = classresult[classCount].ClassName;

                    }

                    classCount++;
                }

                studentCount++;
            }

            return ChangeStudentModelOut(result);
        }

        
        #endregion

下面这个代码是查询班级信息会调用到的。

代码块三:

  
        #region 学生管理公共方法 将查询数据库得到的实体转换成MVC中的model
        /// <summary>
        /// 学生管理公共方法 将查询数据库得到的实体转换成MVC中的model
        /// </summary>
        /// <param name="enstudentList"></param>
        /// <returns></returns>
        public List<Student> ChangeStudentModelOut(List<BasicStudentEntity> enstudentList)
        {
           
            List<Student> studentList = new List<Student>();
            foreach (var item in enstudentList)
            {
                Student student = new Student()
                {
                    
                    StudentID = item.StudentID,
                    StudentNo = item.StudentNo,
                    Sex= item.Sex,
                    Name= item.Name,
<pre name="code" class="csharp">                    ClassName= item.BasicStudentEntity.ClassName,


 }; studentList.Add(student); } return studentList; } #endregion



先大概说一下,改版前自己的实现思路。1查询到学生表中的学生信息。2查询到班级表中的班级信息 3通过循环学生列表和班级列表一一匹配两个表中班级ID相同的数据行,如果相同则将该数据行的班级名称赋值给学生。代码写完了,我也冒冷汗了。(代码二)试想如果现在哪怕只有一个高校在用ITOO,那么假设2万学生,有500个班级的话,查询一次要循环20000*500次。。。


额,我也是醉了。。

后来反复琢磨优化的事,竟然突然明白过来。有人会想问改版后的代码是什么样的?直接删除代码二。 因为在EF中本身就是能够通过一个表的外键去访问到另一个表的实体再去访问另一个表的实体属性。唉, 不怕不知道,就怕不知道。。

EF应用带来的好处

标签:优化   entity framework   

原文地址:http://blog.csdn.net/u010176014/article/details/43279211

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!