简体   繁体   English

C#listbox集合语法

[英]C# listbox Collection syntax

While learning C# pretty fast, I am stumbling on this Collection syntax problem. 虽然学习C#非常快,但我对这个Collection语法问题感到磕磕绊绊。

I added a number of objects of my own type MyItem to listbox lstData. 我将自己类型MyItem的一些对象添加到listbox lstData。 Now I need to search in this listbox and thought of using the elegant LINQ notation like: 现在我需要在这个列表框中搜索并想到使用优雅的LINQ表示法,如:

lstData.Items.Where(x => x.Text == SearchString)

but the Items of a listbox does not have a .Where(), although I did include the "using System.Linq;" 但是列表框的项目没有.Where(),尽管我确实包含了“using System.Linq;” namespace. 命名空间。

So I tried: 所以我尝试过:

foreach (MyItem item in (MyItem)lstData.Items)

but this gives the build error: Cannot convert type 'System.Windows.Forms.ListBox.ObjectCollection' to 'MySandbox.frmListboxDemo.MyItem'. 但这会产生构建错误:无法将类型'System.Windows.Forms.ListBox.ObjectCollection'转换为'MySandbox.frmListboxDemo.MyItem'。

I did manage to write something workable: 我确实设法写了一些可行的东西:

        for (int i = 0; i < lstData.Items.Count; i++)
        {
            MyItem item = (MyItem)lstData.Items[i];
            if (item.Text == SearchString)
            {
                lstData.SetSelected(i, true);
                break;
            }
        }

and a similar version like: 和类似的版本:

        var item_enum = lstData.Items.GetEnumerator();
        while (item_enum.MoveNext()) { etc etc... }

which turned out to be 2 lines longer, and without figuring what could replace 'var'. 结果证明是2行更长,并且没有找出什么可以取代'var'。

I am not really sure I understand how to use Collections, ObjectCollections, Enumerators etc, but am eager to learn. 我不太确定我是否理解如何使用Collections,ObjectCollections,Enumerators等,但我很想学习。 Especially if a .Where() version is possible and/or better. 特别是如果.here()版本是可能的和/或更好的。


Thanks for all your answers. 谢谢你的所有答案。 I ended up with this solution: 我最终得到了这个解决方案:

        var item_iter = lstData.Items.Cast<MyItem>()
                          .Where(x => x.Text.Trim().ToLower() == txtItemName.Text);
        foreach (MyItem item in item_iter)
        {
            int i = lstData.Items.IndexOf(item);
            lstData.SetSelected(i, true);
            break;
        }

I don't know if this is really much better than the "fortran" way (see above), but it does teach me methods I can use in other C# queries. 我不知道这是否真的比“fortran”方式好(见上文),但它确实教会了我可以在其他C#查询中使用的方法。

The items collection is no strongly typed collection. items集合不是强类型集合。 You can use the IEnumerable<T>.OfType() or IEnumerable<T>.Cast() extension methods to get the entry to the LINQ world where you can filter your entries: 您可以使用IEnumerable<T>.OfType()IEnumerable<T>.Cast()扩展方法来获取LINQ世界的条目,您可以在其中过滤条目:

var filteredItems = lstData.Items.OfType<MyItem>().Where(i => i.Prop == val);

The difference between Cast and OfType is: Cast和OfType之间的区别是:

  • OfType<T> will just return the items which are castable to Type T OfType<T>将只返回可转换为T类的项目
  • Cast<T> will fail, when at least one item is in the collection which is not castable to T 如果集合中至少有一个项目不能转换为T,则Cast<T>将失败

Try using the Cast<T> first to perform a cast to the type you expect it to be. 首先尝试使用Cast<T>执行Cast<T>转换为您期望的类型。 That is: 那是:

lstData.Items.Cast<MyItem>().Where(x => x.Text == SearchString)

ListBox.ObjectCollection implements IEnumerable , but not the generic IEnumerable<T> , which most Linq methods require. ListBox.ObjectCollection实现IEnumerable ,但不是大多数Linq方法所需的通用IEnumerable<T>

One way to get to IEnumerable<T> AND solve your casting issue in one step is to use Cast<T> : 获取IEnumerable<T>并一步解决您的转换问题的一种方法是使用Cast<T>

lstData.Items.Cast<MyItem>().Where(x => x.Text == SearchString)

Try to make a list out of the items: 尝试从项目中列出一个列表:

(new ListBox()).Items.OfType<ListViewItem>().Where(......); 

Probably the reason is because it was originally built with earlier version of .net 可能原因是因为它最初是使用早期版本的.net构建的

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

相关问题 C#Collection Initializer现有集合的语法 - C# Collection Initializer Syntax to an existing collection c#winform列表框将system.collection添加到列表框 - c# winform listbox adds system.collection to listbox 如何将ListBox.items转换为c#中的数组集合字符串 - How to convert ListBox.items to string of array collection in c# c#如何从Observable集合中获取列表框选择 - c# How to get Listbox selection from Observable Collection 将可观察的集合绑定到asp.net C#中的列表框 - Bind observable collection to listbox in asp.net c# C# 集合计数某个 object 并在将集合循环到列表框或文本框时打印出来 - C# collection count a certain object and print out while looping through the collection into a listbox or textbox 用于自定义矩阵类的 C# 集合初始化语法? - C# Collection initialization syntax for use with custom matrix class? 在方法语法中选择C# - 返回匿名对象的集合 - Select in Method Syntax C# - returns a collection of anonymous object C#集合初始化语法是否避免默认初始化开销 - Does C# Collection Initialization Syntax Avoid Default Initialization Overhead 在c#中启动集合,类似于Java匿名类语法 - Initiate collection in c# similar to Java anonymous class syntax
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM