Let's go

第三节:EF

1.删除要进行判空

public ActionResult DelClassMethod(string gId)
        {
            //根据gId查询对应条目
            var grade = oc.BllSession.IGradeBLL.Entities.Where(a => a.gId == gId).FirstOrDefault();
            var stuInfo = oc.BllSession.IStuInfoBLL.Entities.Where(b => b.gId == gId).FirstOrDefault();
            //进行删除
            if (stuInfo != null)
            {
                oc.BllSession.IStuInfoBLL.DelNo(stuInfo);
            }
            if (grade != null)
            {
                oc.BllSession.IGradeBLL.DelNo(grade);
            }


            int result = oc.BllSession.IGradeBLL.SaveChange();
            if (result > 0)
            {
                return Content("ok");
            }
            else
            {
                return Content("error");
            }
        }
View Code

2.批量删除

 1         public ActionResult DelStuMethod(string idsStr)
 2         {
 3             //进行字符串拆分
 4             string[] str = idsStr.Split(new char[] { ',' });
 5             int[] str_int = Array.ConvertAll(str, int.Parse);
 6             List<int> stuIdList = str_int.ToList();
 7             var stuList = oc.BllSession.IStuInfoBLL.Entities.Where(a => stuIdList.Contains(a.sId)).ToList();
 8             //删除操作
 9             foreach (var item in stuList)
10             {
11                 oc.BllSession.IStuInfoBLL.DelNo(item);
12             }
13             int result = oc.BllSession.IGradeBLL.SaveChange();
14 
15             //int result = oc.BllSession.IStuInfoBLL.DelBy(a => stuIdList.Contains(a.sId));
16             if (result > 0)
17             {
18                 return Content("ok");
19             }
20             else
21             {
22                 return Content("error");
23             }
24         }
View Code

 

posted @ 2018-07-10 11:51  chenze  阅读(107)  评论(0编辑  收藏  举报
有事您Q我