C# Can';无法从列表中删除项目
我尝试在DeleteProduct方法中从产品中删除项,并且remove方法返回true,但所有项仍在列表中,我不知道为什么 正如我所理解的,产品列表在应用程序的第一个开始时创建,在静态构造函数中,它填充了项目。然后在DeleteProduct方法中,其中一个项移除并执行Products方法 请有人解释一下C# Can';无法从列表中删除项目,c#,asp.net-mvc,C#,Asp.net Mvc,我尝试在DeleteProduct方法中从产品中删除项,并且remove方法返回true,但所有项仍在列表中,我不知道为什么 正如我所理解的,产品列表在应用程序的第一个开始时创建,在静态构造函数中,它填充了项目。然后在DeleteProduct方法中,其中一个项移除并执行Products方法 请有人解释一下 public class ProductController : Controller { private static IEnumerable<ProductView
public class ProductController
: Controller
{
private static IEnumerable<ProductViewModel> products = null;
static ProductController()
{
products = new List<ProductViewModel>
{
new ProductViewModel{Id = 0, Description = "Milk", Price = 21.0, IsAvailable = true, LastUpdate = DateTime.Now},
new ProductViewModel{Id = 1, Description = "Bread", Price = 10.10, IsAvailable = false, LastUpdate = DateTime.Now},
new ProductViewModel{Id = 2, Description = "Soure creame", Price = 34.5, IsAvailable = true, LastUpdate = DateTime.Now},
new ProductViewModel{Id = 3, Description = "Chocolate", Price = 31.0, IsAvailable = true, LastUpdate = DateTime.Now},
new ProductViewModel{Id = 4, Description = "Apples", Price = 1.0, IsAvailable = true, LastUpdate = DateTime.Now},
};
}
public ActionResult Products()
{
return View(products);
}
public ActionResult DeleteProduct(int id)
{
var product = products.FirstOrDefault(p => p.Id == id);
if (product != null)
products.ToList().Remove(product);
return RedirectToAction("Products");
}
}
公共类ProductController
:控制器
{
私有静态IEnumerable products=null;
静态产品控制器()
{
产品=新列表
{
新产品视图模型{Id=0,Description=“Milk”,Price=21.0,IsAvailable=true,LastUpdate=DateTime.Now},
新产品视图模型{Id=1,Description=“Bread”,Price=10.10,IsAvailable=false,LastUpdate=DateTime.Now},
新产品视图模型{Id=2,Description=“Soure cream”,Price=34.5,IsAvailable=true,LastUpdate=DateTime.Now},
新产品视图模型{Id=3,Description=“Chocolate”,Price=31.0,IsAvailable=true,LastUpdate=DateTime.Now},
新产品视图模型{Id=4,Description=“Apples”,Price=1.0,IsAvailable=true,LastUpdate=DateTime.Now},
};
}
公共行动成果产品()
{
返回视图(产品);
}
公共操作结果删除产品(内部id)
{
var product=products.FirstOrDefault(p=>p.Id==Id);
如果(产品!=null)
products.ToList().移除(产品);
退货行动(“产品”);
}
}
执行products.ToList()
操作时,您正在创建一个新列表。因此,当您执行products.ToList().Remove(product)
操作时,您将从新列表中删除该产品
你可以这样做:
if (product != null)
{
List<ProductViewModel> productList = products.ToList();
productList.Remove(product);
products = productList;
}
if(产品!=null)
{
List productList=products.ToList();
产品列表。删除(产品);
产品=产品列表;
}
或者,您可以更改,使静态变量
products
的类型为List
,这样您就不必创建新实例。问题就在这一行:
products.ToList().Remove(product);
通过调用ToList()
可以生成一个新列表,并从新创建的列表中删除产品。要使代码正常工作,请将“产品”字段更改为键入List
:
private static List<ProductViewModel> products = null;
考虑更换:
var product = products.FirstOrDefault(p => p.Id == id);
if (product != null)
products.ToList().Remove(product);
private static IEnumerable<ProductViewModel> products = null;
与:
并替换:
var product = products.FirstOrDefault(p => p.Id == id);
if (product != null)
products.ToList().Remove(product);
private static IEnumerable<ProductViewModel> products = null;
private static IEnumerable products=null;
与:
私有静态列表产品=null;
ToList
是不必要的,因为它创建了一个新的列表
,并从该新的列表
中删除了产品
(这基本上是没有意义的)
FirstOrDefault
是不必要的,因为Remove
将处理只有在您遇到问题时才删除此项的操作。代码原样基本上会在移除项目之前检查两次
IEnumerable
与将其保留为“原始”类型(List
)相比没有任何好处,并且使用IEnumerable
可以取消调用Remove
的功能
<强>也可以考虑使用<代码>并发包而不是<代码>列表>。/P>您所做的是可怕的实践(不要使用<代码>静态< /代码>变量),但如果您第一次测试“<代码>产品< /代码>是代码> null <代码>,它会起作用,建议你也读@StephenMuecke,我绝对不会将数据存储在静态变量中,也不会使用静态变量。只是想创建一个测试example@StephenMuecke谢谢你的帮助links@mjwills谢谢你的提示。我会更新答案的。谢谢!我忘了ToList会生成新的列表
private static List<ProductViewModel> products = null;