Linq to Entity 增,删,改,查 语句

一、增

        public void Insert(ExamineeDto item)
        {
            using (var ctx = new PersonnelExamEntities())
            {
                var newItem = new Ex_Examinee
                {
                    #region  赋值操作

                    ExamineeName = item.ExamineeName,
                    Sex = item.Sex,
                    Nation = item.Nation,
                    PoliticsStaus = item.PoliticsStaus,
                    WorkUnit = item.WorkUnit,
                    IdentityCardNumber = item.IdentityCardNumber,
                    PhotoUpload = false,
                    #endregion

                };
                var myitem = ctx.Ex_Examinee.Where(i => i.IdentityCardNumber == item.IdentityCardNumber && i.ExamPlanId==item.ExamPlanId).FirstOrDefault();
                if (myitem == null)
                {
                    ctx.AddToEx_Examinee(newItem);//对Ex_Examinee增加一条记录
                    ctx.SaveChanges();
                    item.ExamineeID = newItem.ExamineeID;
                }
                else
                {
                    item = Fetch(item.ExamineeID);
                }
            }
        }


 

二、删

        //删除单条记录
        public void Delete(int id)
        {
            using (var ctx = new PersonnelExamEntities())
            {
                var data = ctx.Ex_Examinee.Where(o => o.ExamineeID == id).FirstOrDefault();

                if (data == null)
                    throw new Exception("不存在!");

                ctx.DeleteObject(data);
                ctx.SaveChanges();
            }
        }

        public void Delete(string[] args)
        {
            int count = args.Length;
            using (var ctx = new PersonnelExamEntities())
            {
                for (int i = 0; i < count; i++)
                {
                    int id=int.Parse(args[i].ToString());
                    var data = ctx.Ex_Examinee.Where(o => o.ExamineeID == id).FirstOrDefault();

                    if (data == null)
                        throw new Exception("不存在!");

                    ctx.DeleteObject(data);
                    ctx.SaveChanges();
                }
            }
        }

        //删除多条记录
        public void DeleteAll(int ExamPlanId)
       

你可能感兴趣的:(Asp.net,MVC,LINQ,查询)