简体   繁体   English

从列表中删除项目C#

[英]Removing an item from a list c#

I am having trouble with removing an item from a list in c#. 我在从C#的列表中删除项目时遇到麻烦。 My current code is below: 我当前的代码如下:

for (int i = current.Count - 1; i >= 0; i--)
    {
        foreach (ListItem li in sp_list.Items)
        {
            if (li.Text == current[i].uname)
            {
                current.RemoveAt(i);
            }
        }
    }

The aim of the code is to compare the existing items in a list box with newly added items so I know which items have just been added. 该代码的目的是将列表框中的现有项目与新添加的项目进行比较,以便我知道刚刚添加了哪些项目。 So currently I am getting the current list box items from the database (they are stored here, it is a databound list box), entering these into a list and for each of the list items, comparing them with the items in a list box and if they match, remove the item from the list. 因此,当前我正在从数据库中获取当前列表框项目(它们存储在此处,这是一个数据绑定列表框),将它们输入列表中,对于每个列表项,将它们与列表框中的项目进行比较,然后如果它们匹配,请从列表中删除该项目。

Therefore, in the end, if I add two new entries, the list should only be storing the two newly added values. 因此,最后,如果我添加两个新条目,则该列表应仅存储两个新添加的值。

The code does not work as the first item is removed fine, but then, the value of i is greater than current.count - and therefore I get an out of range exception! 该代码无法正常工作,因为删除了第一项很好,但是随后i的值大于current.count-因此我得到了超出范围的异常!

Could someone help me with this issue? 有人可以帮我解决这个问题吗? Apologies about the confusing question, it's hard to explain! 对这个令人困惑的问题表示歉意,这很难解释! Thanks 谢谢

You can do it with Linq. 您可以使用Linq做到这一点。 Not sure if casting to ListItem needed (you can remove it) 不确定是否需要转换为ListItem(可以将其删除)

current.RemoveAll(x => sp_list.Items.Cast<ListItem>()
                              .Any(li => li.Text == x.uname));

Once you've found the matching value, and removed it from the list, you want to break out of the inner loop to check the next item. 找到匹配值并将其从列表中删除后,您想跳出内循环以检查下一项。

        if (li.Text == current[i].uname)
        {
            current.RemoveAt(i);
            break;
        }

How about this: 这个怎么样:

foreach (ListItem li in sp_list.Items) {
    if (current.Contains(li.Text)) {
        current.Remove(li.Text);
    }
}

Your nesting is wrong, I think you wanted, 您的嵌套错误,我想您想要

foreach (ListItem li in sp_list.Items)
{
    for (int i = current.Count - 1; i >= 0; i--)
    {
        if (li.Text == current[i].uname)
        {
            current.RemoveAt(i);
        }
    }
}

alternatively, use linq, 或者,使用linq

// For lookup performance.
var items = new HashSet(sp_list.Items.Select(i => i.text));

current = current.Where(c => !items.Contains(c.uname)).ToList();

RemoveAt之后放置一个break语句,这样您就不会再次删除该项目。

you can travel the list in reverse order and remove items using RemoveAt(i). 您可以按照相反的顺序浏览列表,并使用RemoveAt(i)删除项目。 also for efficiency purposes you may want to put the ListItem texts in a Set so you can don't have to loop though the sp_list.Items for each of your current items. 同样出于效率目的,您可能希望将ListItem文本放入Set中,这样就不必遍历每个当前项目的sp_list.Items。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM