gpt4 book ai didi

.net - ItemCollection 的基本 LINQ 表达式

转载 作者:行者123 更新时间:2023-12-03 07:00:32 29 4
gpt4 key购买 nike

我有一个ItemCollection我想使用 LINQ 进行查询。我尝试了以下(人为的)示例:

var lItem =
from item in lListBox.Items
where String.Compare(item.ToString(), "abc") == true
select item;

Visual Studio 不断告诉我找不到源类型“System.Windows.Controls.ItemCollection”的查询模式的实现。找不到“哪里”。考虑显式指定范围变量“item”的类型。

如何解决这个问题?

最佳答案

这是因为 ItemCollection 只实现了 IEnumerable ,不是IEnumerable<T> .

您需要有效地调用Cast<T>()如果您显式指定范围变量的类型,则会发生以下情况:

var lItem = from object item in lListBox.Items
where String.Compare(item.ToString(), "abc") == 0
select item;

用点表示法来说,这是:

var lItem = lListBox.Items
.Cast<object>()
.Where(item => String.Compare(item.ToString(), "abc") == 0));

当然,如果您对集合中的内容有更好的了解,您可以指定比 object 更具限制性的类型。 .

关于.net - ItemCollection 的基本 LINQ 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1160854/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com