且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

列表项不会删除

更新时间:2023-12-04 20:58:28

您的代码行

 chklist.Remove(t); 



不正确,因为它试图删除项 t (这是 chklist 的code> purchaseResult list)。由于它不是 chklist 的一部分,因此找不到它,因此没有删除任何内容。

要使代码正常工作,您需要删除该项它位于 chklist 中,它只是 poRevice 列表的别名。



代码可能如下所示:

 chklist.Remove(poRecive [counter]); 
break ; // 这是假设RecNo是唯一的。


I have a code

List<PurchaseMain> addList = new List<PurchaseMain>();
           List<PurchaseMain> poRecive = new List<PurchaseMain>();
           List<PurchaseMain> poNewGenerate = new List<PurchaseMain>();
           using (InventoryEntities inventoryEntities = new InventoryEntities())
           {
               var poResult = (from res in inventoryEntities.PORecived_Main
                               select new PurchaseMain { RecNo = res.RefNo }).ToList<PurchaseMain>(); ;
               poRecive = poResult;
               var poNewResult = (from res in inventoryEntities.PONewGenerate_Main
                                  select new PurchaseMain { RecNo = res.RefNo }).ToList<PurchaseMain>();
               poNewGenerate = poNewResult;
               foreach (var t in poNewGenerate)
               {
                   poRecive.Add(t);
               }
               var purchaseResult = (from res in inventoryEntities.Purchase_Main
                                     select new PurchaseMain { RecNo = res.RecivedPO }).ToList<PurchaseMain>(); ;
               var chklist = poRecive;
               foreach (var t in purchaseResult)
               {
                   for (int counter = 0; counter < poRecive.Count; counter++)
                   {
                       if (t.RecNo == poRecive[counter].RecNo)
                       {
                           chklist.Remove(t);
                       }
                   }
                   //foreach (var rt in poRecive)
                   //{

                   //}
               }
               addList = chklist;
           }
           return addList;



when it return, it contain same value..

Your code line
chklist.Remove(t);


is incorrect as it tries to remove the item t (which is an item in the purchaseResult list) from the chklist. As it is not part of the chklist it is not found and so nothing is removed.
To make your code work you need to remove the item that is in the chklist, which is just an alias for the poRevice list.

The code could look like this:

chklist.Remove(poRecive[counter]);
break; // this is assuming RecNo is unique.