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.
我有一个IList,它包含项目(父类优先),它们需要以相反的顺序添加到关系图文档中,以便最后添加父类,绘制在顶部,以便用户首先选择它。
What's the best way to do it? Something better/more elegant than what I am doing currently which I post below..
最好的方法是什么?比我现在正在做的事情更好/更优雅。
4 个解决方案
#1
7
If you have .NET 3.5 you could use LINQ's Reverse?
如果你有。net 3.5,你可以使用LINQ的反向?
foreach(var item in obEvtArgs.NewItems.Reverse())
{
...
}
(Assuming you're talking about the generic IList)
(假设你说的是一般的伊利亚特)
#2
2
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 the System.Linq.Enumerable.Cast
extension method:
根据对戴维答案的评论,以及Gishu最初的答案,您可以使用弱类型的System.Collections。使用System.Linq.Enumerable. enumerable . enumerable进行泛型集合。把扩展方法:
var reversedCollection = obEvtArgs.NewItems
.Cast<IMySpecificObject>( )
.Reverse( );
This removes the noise of both the reverse for
loop, and the as
cast to get a strongly-typed object from the original collection.
这将消除反向for循环和从原始集合中获取强类型对象的as转换的噪声。
#3
0
NewItems is my List here... This is a bit clunky though.
newitem是我的列表……这有点笨拙。
for(int iLooper = obEvtArgs.NewItems.Count-1; iLooper >= 0; iLooper--)
{
GoViewBoy.Document.Add(CreateNodeFor(obEvtArgs.NewItems[iLooper] as IMySpecificObject, obNextPos));
}
#4
0
You don't need LINQ:
你不需要LINQ:
var reversed = new List<T>(original); // assuming original has type IList<T>
reversed.Reverse();
foreach (T e in reversed) {
...
}
#1
7
If you have .NET 3.5 you could use LINQ's Reverse?
如果你有。net 3.5,你可以使用LINQ的反向?
foreach(var item in obEvtArgs.NewItems.Reverse())
{
...
}
(Assuming you're talking about the generic IList)
(假设你说的是一般的伊利亚特)
#2
2
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 the System.Linq.Enumerable.Cast
extension method:
根据对戴维答案的评论,以及Gishu最初的答案,您可以使用弱类型的System.Collections。使用System.Linq.Enumerable. enumerable . enumerable进行泛型集合。把扩展方法:
var reversedCollection = obEvtArgs.NewItems
.Cast<IMySpecificObject>( )
.Reverse( );
This removes the noise of both the reverse for
loop, and the as
cast to get a strongly-typed object from the original collection.
这将消除反向for循环和从原始集合中获取强类型对象的as转换的噪声。
#3
0
NewItems is my List here... This is a bit clunky though.
newitem是我的列表……这有点笨拙。
for(int iLooper = obEvtArgs.NewItems.Count-1; iLooper >= 0; iLooper--)
{
GoViewBoy.Document.Add(CreateNodeFor(obEvtArgs.NewItems[iLooper] as IMySpecificObject, obNextPos));
}
#4
0
You don't need LINQ:
你不需要LINQ:
var reversed = new List<T>(original); // assuming original has type IList<T>
reversed.Reverse();
foreach (T e in reversed) {
...
}