更新时间: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 linechklist.Remove(t);
is incorrect as it tries to remove the itemt
(which is an item in thepurchaseResult
list) from thechklist
. As it is not part of thechklist
it is not found and so nothing is removed.
To make your code work you need to remove the item that is in thechklist
, which is just an alias for thepoRevice
list.
The code could look like this:chklist.Remove(poRecive[counter]); break; // this is assuming RecNo is unique.