如何以相反的顺序迭代 .Net IList 集合?
我有一个包含项目(父项在前)的 IList,它们需要以相反的顺序添加到图表文档中,以便父项最后添加,绘制在顶部,以便它是用户首先选择的内容。
最好的方法是什么? 比我现在所做的更好/更优雅的事情 我在下面发布的..
I have an IList that contains items ( parent first ), they need to be added to a Diagram Document in the reverse order so that the parent is added last, drawn on top so that it is the first thing to be selected by the user.
What's the best way to do it? Something better/more elegant than what I am doing currently
which I post below..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
NewItems 是我的列表...不过这有点笨重。
NewItems is my List here... This is a bit clunky though.
你不需要 LINQ:
You don't need LINQ:
如果您有 .NET 3.5,您可以使用 LINQ 的 Reverse 吗?
(假设您正在谈论通用 IList)
If you have .NET 3.5 you could use LINQ's Reverse?
(Assuming you're talking about the generic IList)
基于对Davy的回答的评论,以及Gishu 的原始答案,您可以使用
System.Linq 将弱类型
扩展方法:System.Collections.IList
转换为通用集合.Enumerable.Cast这消除了反向
for
循环和as
转换的噪音,以从原始对象获取强类型对象收藏。Based on the comments to Davy's answer, and Gishu's original answer, you could cast your weakly-typed
System.Collections.IList
to a generic collection using theSystem.Linq.Enumerable.Cast
extension method:This removes the noise of both the reverse
for
loop, and theas
cast to get a strongly-typed object from the original collection.