首页 > 解决方案 > 从列表中删除一系列项目而不循环

问题描述

嗨,有没有更优雅的方式来做这件事我必须做循环是否像范围功能一样我可以删除所有找到的项目

抱歉,我应该展示我的 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 中删除的已删除espatchitems 中删除项目我不知道为什么

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;
}

标签: c#listlinqcollections

解决方案


不是 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();

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


推荐阅读