目录
1.1.5、 //从某集合中删除其与另一个集合中相同的项;其实这个说简单点就是某集合中独有的元素(差集)
1.1.11、 从某集合中删除其与另一个集合中相同的项;其实这个说简单点就是某集合中独有的元素(差集)
using QXQPS.Models;
using QXQPS.Vo;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace QXQPS.Areas.MechanicsManagment.Controllers
{
public class AppointmentController : Controller
{
// GET: MechanicsManagment/Appointment
Models.QXQPEntities myModels = new Models.QXQPEntities();
public ActionResult ListPredate(List<PW_Predate> listPredate, List<SYS_PreRepairItemDetail> listPreRepairItem,List<SYS_PreProductDetail> listPreProduct,
List<SYS_PreOtherCostDetail> listPreOtherCost )//
{
var PredateID = 0;
try
{
if (listPredate[0].PredateID == 0)
{
myModels.PW_Predate.Add(listPredate[0]);
}
else
{
myModels.Entry(listPredate[0]).State = System.Data.Entity.EntityState.Modified;
}
if (myModels.SaveChanges() > 0)
{
PredateID = listPredate[0].PredateID;
if (listPreRepairItem != null)
{ //
List<int> oldID = new List<int>();//原来ID
List<int> newID = new List<int>();//新ID
List<int> listdelectID = new List<int>();//需要删除的ID集合
var list = myModels.SYS_PreRepairItemDetail.Where(m => m.PredateID == PredateID).Select(m => new { m.PreRepairItemDetailID }).ToList();
foreach (var item in list)
{
oldID.Add(item.PreRepairItemDetailID);
}
for (int i = 0; i < listPreRepairItem.Count; i++)
{
listPreRepairItem[i].PredateID = PredateID;
if (listPreRepairItem[i].PreRepairItemDetailID == 0)
{
myModels.SYS_PreRepairItemDetail.Add(listPreRepairItem[i]);//新增
}
else
{
newID.Add(listPreRepairItem[i].PreRepairItemDetailID);
myModels.Entry(listPreRepairItem[i]).State = System.Data.Entity.EntityState.Modified;//修改
}
}
listdelectID = oldID.Except(newID).ToList();//从某集合中删除其与另一个集合中相同的项;其实这个说简单点就是某集合中独有的元素(差集)
foreach (var item in listdelectID)
{
var listdelect = myModels.SYS_PreRepairItemDetail.Where(m => m.PreRepairItemDetailID == item).Single();//删除
myModels.SYS_PreRepairItemDetail.Remove(listdelect);
}
}
else {
var listdelect = myModels.SYS_PreRepairItemDetail.Where(m => m.PredateID == PredateID).ToList();//删除全部
myModels.SYS_PreRepairItemDetail.RemoveRange(listdelect);
}
if (listPreProduct != null)
{
List<int> oldID = new List<int>();//原来ID
List<int> newID = new List<int>();//新ID
List<int> listdelectID = new List<int>();//需要删除的ID集合
var list = myModels.SYS_PreProductDetail.Where(m => m.PredateID == PredateID).Select(m => new { m.PreProductDetailID }).ToList();
foreach (var item in list)
{
oldID.Add(item.PreProductDetailID);
}
for (int i = 0; i < listPreProduct.Count; i++)
{
listPreProduct[i].PredateID = PredateID;
if (listPreProduct[i].PreProductDetailID == 0)
{
myModels.SYS_PreProductDetail.Add(listPreProduct[i]);//新增
}
else
{
newID.Add(listPreProduct[i].PreProductDetailID);
myModels.Entry(listPreProduct[i]).State = System.Data.Entity.EntityState.Modified;//修改
}
}
listdelectID = oldID.Except(newID).ToList();
foreach (var item in listdelectID)
{
var listdelect = myModels.SYS_PreProductDetail.Where(m => m.PreProductDetailID == item).Single();
myModels.SYS_PreProductDetail.Remove(listdelect);
}
}
else
{
var listdelect = myModels.SYS_PreProductDetail.Where(m => m.PredateID == PredateID).ToList();//
myModels.SYS_PreProductDetail.RemoveRange(listdelect);
}
if (listPreOtherCost != null)
{
List<int> oldID = new List<int>();//原来ID
List<int> newID = new List<int>();//新ID
List<int> listdelectID = new List<int>();//需要删除的ID集合
var list = myModels.SYS_PreOtherCostDetail.Where(m => m.PredateID == PredateID).Select(m => new { m.PreOtherCostDetailID }).ToList();
foreach (var item in list)
{
oldID.Add(item.PreOtherCostDetailID);
}
for (int i = 0; i < listPreOtherCost.Count; i++)
{
listPreOtherCost[i].PredateID = PredateID;
if (listPreOtherCost[i].PreOtherCostDetailID == 0)
{
myModels.SYS_PreOtherCostDetail.Add(listPreOtherCost[i]);//
}
else
{
newID.Add(listPreOtherCost[i].PreOtherCostDetailID);
myModels.Entry(listPreOtherCost[i]).State = System.Data.Entity.EntityState.Modified;//
}
}
listdelectID = oldID.Except(newID).ToList();//
foreach (var item in listdelectID)
{
var listdelect = myModels.SYS_PreOtherCostDetail.Where(m => m.PreOtherCostDetailID == item).Single();//删除
myModels.SYS_PreOtherCostDetail.Remove(listdelect);
}
}
else
{
var listdelect = myModels.SYS_PreOtherCostDetail.Where(m => m.PredateID == PredateID).ToList();//
myModels.SYS_PreOtherCostDetail.RemoveRange(listdelect);
}
myModels.SaveChanges();
}
else
{
return Json(false, JsonRequestBehavior.AllowGet);
}
}
catch (Exception)
{
return Json(false, JsonRequestBehavior.AllowGet);
}
return Json(PredateID, JsonRequestBehavior.AllowGet);
}