列表项不会删除 [英] List Items not removes

查看:63
本文介绍了列表项不会删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码

列表< PurchaseMain> addList =  new  List< PurchaseMain>(); 
列表< PurchaseMain> poRecive = new 列表< PurchaseMain>();
列表< PurchaseMain> poNewGenerate = new List< PurchaseMain>();
使用(InventoryEntities inventoryEntities = new InventoryEntities())
{
var poResult =(来自 res inventoryEntities.PORecived_Main
选择 new PurchaseMain {RecNo = res.RefNo})。ToList< PurchaseMain> ;(); ;
poRecive = poResult;
var poNewResult =( from res in inventoryEntities.PONewGenerate_Main
选择 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
选择 new PurchaseMain {RecNo = res .RecivedPO})。ToList< PurchaseMain>(); ;
var chklist = poRecive;
foreach var t in purchaseResult)
{
for int counter = 0 ;计数器< poRecive.Count; counter ++)
{
if (t.RecNo == poRecive [counter] .RecNo)
{
chklist.Remove(t);
}
}
// foreach(var rt in poRecive)
// {

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





返回时,它包含相同的值..

解决方案

您的代码行

 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.


这篇关于列表项不会删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆