C# 从列表中删除项的方法

C# 从列表中删除项的方法,c#,list,C#,List,与此相关: 如何为下面的类构建方法,该类将根据其某个字段的值从列表中删除项 public class FakeProductsRepository { private readonly List<Product> fakeProducts = new List<Product> { new Product { ProductID = "xxx", Description = "xxx", Price = 1000 }, new Produ

与此相关:

如何为下面的类构建方法,该类将根据其某个字段的值从列表中删除项

public class FakeProductsRepository 
{
  private readonly List<Product> fakeProducts = new List<Product>
  {
      new Product { ProductID = "xxx", Description = "xxx", Price = 1000 },
      new Product { ProductID = "yyy", Description = "xxx", Price = 2000 },
      new Product { ProductID = "zzz", Description = "xxx", Price = 3000 },
  };

  public void AddProduct(string productID, string description, int price)
  {
      fakeProducts.Add(new Product
      {
          ProductID = productID,
          Description = description,
          Price = price,
      });
  }

  public void RemoveProduct(string productID)
  {
      ????????
      //How to remove the item from the fakeProducts List where ProductID == productID?
  }

  public IQueryable<Product> Products
  {
      get { return fakeProducts.AsQueryable(); }
  }
}
public-class-fakeproducts-repository
{
私有只读列表fakeProducts=新列表
{
新产品{ProductID=“xxx”,Description=“xxx”,价格=1000},
新产品{ProductID=“yyy”,Description=“xxx”,Price=2000},
新产品{ProductID=“zzz”,Description=“xxx”,Price=3000},
};
public void AddProduct(字符串productID、字符串描述、整数价格)
{
假冒产品。添加(新产品)
{
ProductID=ProductID,
描述=描述,
价格=价格,
});
}
public void RemoveProduct(字符串productID)
{
????????
//如何从ProductID==ProductID的fakeProducts列表中删除该项?
}
公共卫生产品
{
获取{return fakeProducts.AsQueryable();}
}
}
问题方法是用“???????”和注释字符串指出的。

尝试使用。注意,您需要.NET3.5

var reducedProducts = from r in fakeProducts
                      where r.ProductID != productID
                      select r;
这将减少您的收藏量

fakeProducts.RemoveAll(delegate (Product r) { return r.ProductID != productID; });
您还可以使用该方法,该方法接受谓词,但修改当前集合

fakeProducts.RemoveAll(delegate (Product r) { return r.ProductID != productID; });
尝试使用。注意,您需要.NET3.5

var reducedProducts = from r in fakeProducts
                      where r.ProductID != productID
                      select r;
这将减少您的收藏量

fakeProducts.RemoveAll(delegate (Product r) { return r.ProductID != productID; });
您还可以使用该方法,该方法接受谓词,但修改当前集合

fakeProducts.RemoveAll(delegate (Product r) { return r.ProductID != productID; });

通常,对于集合,我会使用以下代码:

var productsToRemove = fakeProducts.Where(p => p.ProductID == productID).ToList();
foreach(var product in productsToRemove)
{
   fakeProducts.Remove(product);
}
不要忘记
ToList()
,否则您会得到一个
InvalidOperationException
,上面写着“集合已修改”

更新(感谢linuxuser27): 但是
列表
还有一个特殊的方法,采用
谓词


通常,对于集合,我会使用以下代码:

var productsToRemove = fakeProducts.Where(p => p.ProductID == productID).ToList();
foreach(var product in productsToRemove)
{
   fakeProducts.Remove(product);
}
不要忘记
ToList()
,否则您会得到一个
InvalidOperationException
,上面写着“集合已修改”

更新(感谢linuxuser27): 但是
列表
还有一个特殊的方法,采用
谓词


如果按产品ID查找项目是常见的操作,请考虑用产品ID键入的字典替换列表。

但是,如果希望将列表保留为数据结构,则只需对其进行循环(这假设只有一个产品具有给定的ProductID):


如果按产品ID查找项目是常见的操作,请考虑用产品ID键入的字典替换列表。

但是,如果希望将列表保留为数据结构,则只需对其进行循环(这假设只有一个产品具有给定的ProductID):


这将引发InvalidOperationException(“集合已修改”)。不,不会。在修改之后,在继续迭代之前,您就可以突破了。这样就没有理由使用foreach了。只要调用ContainsKey,如果返回true,则调用Remove。@Jim:我可能不清楚。我建议将列表交换到字典作为一个选项(如您所述,使用ContainsKey/remove实现进行删除),但是对于列表,除了遍历之外,没有明确的方法来查看其中的产品ID。我的错。看来我没有完全阅读你的回答。我现在明白了,你们的第二句话是关于列表的。(虽然您可能希望将“链接列表”更改为“列表”),但这将引发InvalidOperationException(“集合已修改”)。不,不会。在修改之后,在继续迭代之前,您就可以突破了。这样就没有理由使用foreach了。只要调用ContainsKey,如果返回true,则调用Remove。@Jim:我可能不清楚。我建议将列表交换到字典作为一个选项(如您所述,使用ContainsKey/remove实现进行删除),但是对于列表,除了遍历之外,没有明确的方法来查看其中的产品ID。我的错。看来我没有完全阅读你的回答。我现在明白了,你们的第二句话是关于列表的。(尽管您可能希望将“链接列表”更改为“列表”),但请注意,RemoveAll是.NET 4中的一个新方法,您不需要删除
只读
即可使用它。只读“仅对字段引用本身有效,但对列表内容无效。@Yacoder这不是真的
RemoveAll()
自.NET 2.0以来已经存在了
List
。检查上面的API链接。在
只读
上的调用很好。我修改了我的答案。哦,对了。然后您应该能够编写fakeProducts.RemoveAll(product=>product.ProductID==ProductID);应该注意的是,RemoveAll是.NET4中的一个新方法,使用它不需要删除
readonly
。只读“仅对字段引用本身有效,但对列表内容无效。@Yacoder这不是真的
RemoveAll()
自.NET 2.0以来已经存在了
List
。检查上面的API链接。在
只读
上的调用很好。我修改了我的答案。哦,对了。然后您应该能够编写fakeProducts.RemoveAll(product=>product.ProductID==ProductID);