从列表中删除一系列项目而不循环

时间:2018-10-31 11:00:02

标签: c# list linq collections

嗨,有没有一种更优雅的方法可以执行此操作?我必须执行循环操作,就像使用范围函数一样,我可以删除找到的所有项

对不起,我应该演示如何插入我的qry。

这是我要删除的两个不同的实体,希望您能明白。

var qry = db.AssemblyListItems.AsNoTracking().Where(x => 
x.ProductionPlanID == (long)_currentPlan.ProductionPlan ).ToList();

var hasbeenAssembled = db.CompletedPrinteds.AsNoTracking().Where(x =>
x.ProductionPlanId == item.ProductionPlanID).ToList();

var hasbeenFound = db.CompletedPrinteds.AsNoTracking().Where(x => 
x.ProductionPlanId== item.ProductionPlanID).ToList();

foreach (var subitem in hasbeenAssembled )
{
  if(item.ProductionPlanID ==subitem.ProductionPlanId && item.DocumentNo == subitem.DocumentNo && item.DocumentNo == subitem.DocumentNo && item.OutstandingToMake ==0)
    {
      qry.RemoveAll(x => x.ProductionPlanID == subitem.ProductionPlanId && x.DocumentNo == item.DocumentNo && x.ItemCode == subitem.StockCode && item.OutstandingToMake ==0);                   

    }
}

public List<AssemblyListItems>  RemoveDespatchedItems(List<AssemblyListItems> AssemblyItems)
{       
        foreach (AssemblyListItems item in AssemblyItems)
        {
            using (var db = new LiveEntities())
            {
                var hasNotBeenDespatched = db.PalletizedItems.Where(w => w.Despatched != "Not Despatched");
                foreach (var subitem in hasNotBeenDespatched)
                {
               AssemblyItems.RemoveAll(x => x.ProductionPlanID == subitem.ProductionPlanID && x.DocumentNo == item.DocumentNo && x.ItemCode == subitem.StockCode);
                }


            }
        }
        return AssemblyItems;
}

我只需要从第二个查询中删除第一个查询hasNotBeenDespatched中的项目。我可能希望超过400个项目才能尽可能高效。

编辑2  我有点接近,谢谢,但是它仍然没有从Assebmittems的removespatchitems中删除项目,我不不为什么

public List<AssemblyListItems> RemoveDespatchedItems(List<AssemblyListItems> AssemblyItems, Int64 ProductionPlanId)
{
   using (var db = newLiveEntities())
   {
            List<PalletizedItems> removeDespatchItems = db.PalletizedItems.Where(w => w.Despatched != "Not Despatched" && w.ProductionPlanID == ProductionPlanId).ToList();

            var itemsDocumentNo = db.PalletizedItems.Select(x => x.ProductionPlanItemID).ToList();        
            foreach (var subitem in removeDespatchItems)  {                 

                AssemblyItems.RemoveAll(x => x.ProductionPlanID == subitem.ProductionPlanID && itemsDocumentNo.Contains(x.ProductionPlanItemID) && x.ItemCode == subitem.StockCode && x.LineQuantity==x.AllocatedQuantity);

        }
    }

        return AssemblyItems;
}

1 个答案:

答案 0 :(得分:0)

不是100%我确切地知道应该怎样。

但是,一般而言,您可以使用join来导致它在数据库中完成。像这样:

var remainingItems = (from ali in db.FUEL_AssemblyListItems
            join completed in db.FuelCompletedPrinteds 
                on new { ali.ProductionPlanID, ali.DocumentNo, ali.ItemCode } equals new { completed.ProductionPlanID, completed.DocumentNo, completed.StockCode }
            join dispatched in db.FUEL_PalletizedItems
                on new { ali.ProductionPlanID, ali.DocumentNo, ali.ItemCode } equals new { dispatched.ProductionPlanID, dispatched.DocumentNo, dispatched.StockCode }
            where (ali.ProductionPlanID == (long) _currentPlan.ProductionPlan
                && ali.DocumentNo == completed.DocumentNo
                && completed.OutstandingToMake == 0
                && dispatched.Despatched != "Not Despatched")
            select ali).ToList();

根据数据库中的记录,联接可能需要是外部联接,需要稍微不同的语法,但希望您有一个起点。