从列表中删除项目-C# [英] Removing an item from a list - C#

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

问题描述

我的数据结构比较复杂.简单版本如下:

I have rather a complex datastructure. The simple version looks like this:

public class Field
{

  List<FieldRow> fieldRow;

  //I want to write a delete that iterates and deletes given the key 
  //(Use Linq?)
  public void DeleteByKey(int key)
  {
    //Do Remove
  }
}

public class FieldRow
{
  public int key;
}

感谢您对实现删除的任何帮助.

Any help in implementing Delete is appreciated.

推荐答案

public class Field
{
  List<FieldRow> fieldRow;

  public void DeleteByKey(int key)
  {
    fieldRow.RemoveAll(row => key == row.key);
  }
}

public class FieldRow
{
  public int key;
}

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

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