M. *_*ley 42 .net linq itemcollection
我有一个ItemCollection我想使用LINQ查询.我尝试了以下(人为的)示例:
var lItem =
from item in lListBox.Items
where String.Compare(item.ToString(), "abc") == true
select item;
Run Code Online (Sandbox Code Playgroud)
Visual Studio一直告诉我 Cannot find an implementation of the query pattern for source type 'System.Windows.Controls.ItemCollection'. 'Where' not found. Consider explicitly specifying the type of the range variable 'item'.
我该如何解决这个问题?
Jon*_*eet 84
这是因为ItemCollection只实现IEnumerable,而不是IEnumerable<T>.
Cast<T>()如果明确指定范围变量的类型,则需要有效地调用将发生的情况:
var lItem = from object item in lListBox.Items
where String.Compare(item.ToString(), "abc") == 0
select item;
Run Code Online (Sandbox Code Playgroud)
以点表示法,这是:
var lItem = lListBox.Items
.Cast<object>()
.Where(item => String.Compare(item.ToString(), "abc") == 0));
Run Code Online (Sandbox Code Playgroud)
如果当然,如果您对集合中的内容有更好的了解,则可以指定比限制更严格的类型object.
| 归档时间: |
|
| 查看次数: |
15103 次 |
| 最近记录: |