[英]Better way to write this code rather than 2x foreach?
List<CormantRadPane> panesToSave = new List<CormantRadPane>();
foreach (KeyValuePair<string, RadPaneSetting> paneState in RadControlStates.PaneStates)
{
CormantRadPane pane = Utilities.FindControlRecursive(Page, paneState.Key) as CormantRadPane;
panesToSave.Add(pane);
}
foreach (CormantRadPane pane in panesToSave)
{
RadControlSave.SavePane(pane);
}
RadControlSave.SavePane(pane) modifies the RadControlStates.PaneStates collection. RadControlSave.SavePane(pane)修改RadControlStates.PaneStates集合。 Is there a nicer way to write this code in this situation?
在这种情况下,有没有更好的方法来编写此代码?
EDIT: Everyone please read what I wrote literally right above this. 编辑:每个人都请阅读我在此上方写的内容。 Jimmy posted an obvious solution that I hadn't though about -- creating a copy of the collection right before iterating through it so that RadControlSave does not modify the collection being iterated upon.
Jimmy发布了一个我还没有解决的明显解决方案-在遍历集合之前立即创建该集合的副本,以使RadControlSave不会修改正在迭代的集合。
one way would be to call ToArray() on PaneStates (to make a copy, because you can't modify the sequence inside the foreach) 一种方法是在PaneStates上调用ToArray()(进行复制,因为您无法修改foreach内部的序列)
foreach(var state in RadControlStates.PaneStates.ToArray()) {
var pane = Utilities.FindControlRecursive(Page, state.Key) as CormantRadPane;
RadControlSave.SavePane(pane);
}
Since calling RadControlSave.SavePane() modifies the collection, there's really no better way to do this. 由于调用RadControlSave.SavePane()会修改集合,因此确实没有更好的方法。
Considering there's nothing fundamentally wrong with this code, I wouldn't worry about it too much. 考虑到此代码从根本上没有什么错,所以我不必为此担心太多。 You could use LINQ, though, to make it look a little cleaner (even though it would be doing the same fundamental thing underneath):
但是,您可以使用LINQ使它看起来更整洁(即使它在下面做相同的基本操作):
var panesToSave = RadControlStates.PaneStates
.Select(ps => Utilities.FindControlRecursive(Page, ps.Key) as CormantRadPane)
.ToList();
foreach(var pane in panesToSave)
{
RadControlSave.SavePane(pane);
}
I'm not familiar with any of those static methods though. 我对这些静态方法都不熟悉。 If there's a way you could rewrite them to not modify the state of the collection...that would be preferable (it would allow you to do all the work in a single loop).
如果有一种方法可以重写它们而不修改集合的状态...那将是更好的选择(它将允许您在单个循环中完成所有工作)。
Update 更新资料
You could also clone the original collection and combine the work in a single loop: 您还可以克隆原始集合并将工作合并到一个循环中:
foreach(var kvp in RadControlStates.PaneStates.ToList())
{
RadControlSave.SavePane(
Utilities.FindControlRecursive(Page, paneState.Key) as CormantRadPane
);
}
Can't you just do the SavePane()
in the first foreach
? 您不能只在第一个
foreach
执行SavePane()
吗?
foreach (KeyValuePair<string, RadPaneSetting> paneState in RadControlStates.PaneStates)
{
CormantRadPane pane = Utilities.FindControlRecursive(Page, paneState.Key) as CormantRadPane;
RadControlSave.SavePane(pane);
}
Can't you just combine them? 你不能把它们结合起来吗?
List<CormantRadPane> panesToSave = new List<CormantRadPane>();
foreach (KeyValuePair<string, RadPaneSetting> paneState in RadControlStates.PaneStates)
{
CormantRadPane pane = Utilities.FindControlRecursive(Page, paneState.Key) as CormantRadPane;
RadControlSave.SavePane(pane);
panesToSave.Add(pane);
}
How about this: 这个怎么样:
List<CormantRadPane> panesToSave = new List<CormantRadPane>();
foreach (KeyValuePair<string, RadPaneSetting> paneState in RadControlStates.PaneStates)
{
CormantRadPane pane = Utilities.FindControlRecursive(Page, paneState.Key) as CormantRadPane;
//panesToSave.Add(pane);
RadControlSave.SavePane(pane);
}
我认为您可以将其保存,而不是添加它(在第一个foreach中)
Do you have a collection of the Panes you're trying to save (instead of FindControlRecursive?) 您是否有要保存的窗格的集合(而不是FindControlRecursive?)
Then you could use LINQ on that collection like: 然后,您可以在该集合上使用LINQ,例如:
foreach(var pane in Panes.Where(p => p.IsPane))
{
RadControlSave.SavePane(pane);
}
Is the panesToSave List neccessary why not simply have: 为什么不是必须要有panesToSave List?
foreach (KeyValuePair<string, RadPaneSetting> paneState in RadControlStates.PaneStates)
{
CormantRadPane pane = Utilities.FindControlRecursive(Page, paneState.Key) as CormantRadPane;
RadControlSave.SavePane(pane);
}
Hope This Helps, Eamonn 希望这会有所帮助,Eamonn
How about this 这个怎么样
foreach (var pane in
RadControlStates.PaneStates
.Select(paneState => Utilities
.FindControlRecursive(Page, paneState.Key) as CormantRadPane))
RadControlSave.SavePane(pane);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.