Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Listbox.Contains与“包含”;例如;操作人员_C#_Winforms - Fatal编程技术网

C# Listbox.Contains与“包含”;例如;操作人员

C# Listbox.Contains与“包含”;例如;操作人员,c#,winforms,C#,Winforms,我在winform上有一个列表框,其中包含数百个项目。我想删除所有那些有“颜色”一词的项目 例如,下面是我的列表框中的一些项目: [0]Weight [1]Height [2]Eye Color [3]Hair Color [4]Discoloration [5]Type 因此,[2][3][4]将从此列表框中删除。(不区分大小写) 到目前为止,我的代码是: //From multiple sources foreach (string line in sometextfiles) {

我在winform上有一个列表框,其中包含数百个项目。我想删除所有那些有“颜色”一词的项目

例如,下面是我的列表框中的一些项目:

[0]Weight
[1]Height
[2]Eye Color
[3]Hair Color
[4]Discoloration
[5]Type
因此,
[2][3][4]
将从此列表框中删除。(不区分大小写)

到目前为止,我的代码是:

//From multiple sources
foreach (string line in sometextfiles)
{
    lstNames.Items.Add(line)
}

foreach (string line in othertextfiles)
{
    if (!lstNames.Items.Contains(line))
    lstNames.Items.Add(line)
}

foreach (string line in moretextfiles)
{
    if (!lstNames.Items.Contains(line))
    lstNames.Items.Add(line)
}

foreach (string line in evenmoretextfiles)
{
    if (!lstNames.Items.Contains(line))
    lstNames.Items.Add(line)
}

//Finally I want to remove unwanted items
var query = (from x in lstNames.Items
                     where x.Contains("color")
                     select x);

您可以将LINQ与
ListBox.Items
一起使用,但您必须先强制转换
Items
。试试这个:

listBox.Items.Cast<object>()
    .Where(x => x.ToString().IndexOf("color", StringComparison.CurrentCultureIgnoreCase) >= 0)
    .ToList()
    .ForEach(x => listBox.Items.Remove(x));
listBox.Items.Cast()
.Where(x=>x.ToString().IndexOf(“color”,StringComparison.CurrentCultureIgnoreCase)>=0)
托利斯先生()
.ForEach(x=>listBox.Items.Remove(x));
根据您向列表框中添加项目的方式,您可以将其替换为
Cast()
,并删除
ToString()

ListBox.Items.ToList().RemoveWhere(x=>x.stringComponent.Contains(“颜色”);
你需要告诉x它是什么类型的

看起来您需要删除where扩展方法

public static void RemoveWhere<T>(this IList<T> source, Func<T, bool> predicate)
    {
        for (int i = 0; i < source.Count; i++)
        {
            if (predicate(source[i]))
            {
                source.Remove(source[i]);
                i--;
            }
        }
    }
公共静态void removehere(此IList源,Func谓词)
{
for(int i=0;i
我尝试了一些LINQ查询,比如
。其中(x=>x.Contains(“/color/”)
但是它说不能在Listbox上实现LINQ查询,Listbox上也不能实现LINQ查询。items@Delta是的,我收到一条错误消息,说
error 1找不到源类型“System.Windows.Forms.ListBox.ObjectCollection”的查询模式的实现找不到的地方请显示完整的代码片段,以便我们可以清楚地看到您在一个地方尝试了什么。另外,您使用的是Visual Studio和.NET的哪些版本?为什么不先过滤掉“颜色”字符串,然后再添加它们(在从多个文件中添加的初始行中)?这对我来说并不适用
ListBox.Items
'与
removehere
不兼容。符合我的要求。但这是因为它是我自己代码库中的一个扩展方法。我不知道。您有一个扩展方法隐藏在某处。@pasty,是的。因此,我注意到:)。但是,如果您添加了字符串以外的任何内容,则必须强制转换到
对象
并调用
ToString()
。谢谢。我稍微修改了你的代码以满足我的需求,但它解决了我问题的主要部分。
ListBox.Items.RemoveWhere(x => ((MyType)x).stringComponent.Contains("color"));
ListBox.Items.ToList<MyType>().RemoveWhere(x => x.stringComponent.Contains("color"));
public static void RemoveWhere<T>(this IList<T> source, Func<T, bool> predicate)
    {
        for (int i = 0; i < source.Count; i++)
        {
            if (predicate(source[i]))
            {
                source.Remove(source[i]);
                i--;
            }
        }
    }