日期:2014-05-20  浏览次数:21039 次

entity framework比LinQ还慢那?
本地项目环境:
VS2010
.net framework 4.0
mysql 5.0
项目采用webform

下面是主要函数的代码,通过查询2次数据库取到本人对应的模块信息,但是发现每次点击都会延迟3秒左右。
edmx 我是通过vs2010的自动生成实现的,感觉EF比LinQ To SQL 慢好多
C# code

private EFEntities db = new EFEntities();
/// <summary>
        /// 获取模块列表
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public List<models> FindModels(int uid, int modelId)
        {
            var model_list = from p in db.model_in_role
                             join r in db.teacher_in_role on p.RoleId equals r.RoleId
                             where r.Uid == uid
                             select p.ModelIdList;

            List<models> models = new List<models>();
            if (model_list.Count() > 0)
            {
                //显示数据
                string[] mms = model_list.First().Split(',');
                List<int> alModelId = new List<int>();
                foreach (var mm in mms)
                {
//StringHelper.StringToInt 是把string 转成 int
                    int mId = StringHelper.StringToInt(mm);
                    if (mId > 0)
                    {
                        alModelId.Add(mId);
                    }
                }

                var ms = from p in db.models
                         where alModelId.Contains(p.ModelId) && p.Pid == modelId
                         select p;

                models = ms.ToList();
            }

            return models;
        }



------解决方案--------------------
C# code

                var ms = from p in db.models
                         where alModelId.Contains(p.ModelId) && p.Pid == modelId
                         select p;

                models = ms.ToList();