MenuItemCollection 与 List
最近,我使用 Lambda 表达式编写了一段 C# 代码:
var dynMenu = new List<MenuItem>();
// some code to add menu items to dynMenu
if (!dynMenu.Any(x => x.Text == controller))
{
// do something
}
在检查我的代码时,我发现每个 MenuItem 本身都有一个名为 ChildItems 的属性,该属性恰好是 MenuItemCollection 类型。出于好奇,我想我可以用这个 MenuItemCollection 替换我的 MenuItem 列表。
将第一行更改为:
var dynMenu = new MenuItemCollection();
我注意到这个 MenuItemCollection 类型没有像“Any<>”、“All<>”、“First<>”等扩展方法 - 我觉得很奇怪。
有没有办法在这里使用 Lambda 表达式?
我应该继续使用“List<<\MenuItem>”吗?
Recently I wrote a piece of C# code utilizing a Lambda expression:
var dynMenu = new List<MenuItem>();
// some code to add menu items to dynMenu
if (!dynMenu.Any(x => x.Text == controller))
{
// do something
}
While going thru my code, I discovered that each MenuItem itself has a property called ChildItems which happens to be of type MenuItemCollection. Intrigued, I figured I would replace my List of MenuItem with this MenuItemCollection.
Upon changing the first line to:
var dynMenu = new MenuItemCollection();
I noticed that this MenuItemCollection type has no Extension Methods like "Any<>", "All<>", "First<>", etc., etc. -- which I find strange.
Is there a way to utilize Lambda expressions here?
Should I just go back to using "List<<\MenuItem>"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
MenuItemCollection 是 .NET 1.1 类。它不实现任何通用集合接口,特别是
IEnumerable
。由于这仅实现 IEnumerable 而不是 IEnumerable
Cast 扩展方法将
IEnumerable
到IEnumerable
,提供对其他 LINQ 扩展方法的访问。MenuItemCollection is a .NET 1.1 class. It does not implement any of the generic collection interfaces, in particular,
IEnumerable<MenuItem>
.Since this only implements IEnumerable and not
IEnumerable<MenuItem>
, all of the extension methods LINQ provides which require this don't work. However, you can get them back by doing:The Cast extension method converts the
IEnumerable
toIEnumerable<T>
, providing access to the other LINQ extension methods.MenuItemCollection 是一个专门的集合,不实现 IEnumerable 接口来使用您在上面指定的 Linq 扩展方法。我只想回到通用列表,因为它有更多功能
MenuItemCollection is a specialized collection and does not implement the IEnumerable Interface to use the Linq Extension Methods that you name above. I would just go back to the generic list as it has more functionality