并排枚举编码的其他方法?
我想知道是否有人可以向我展示该方法的其他编写方式,也许使用 LINQ?
private static bool CompareManyFoos(ManyFoos expected, ManyFoos actual)
{
IEnumerator<Foo> expFooAtor = expected.GetEnumerator();
IEnumerator<Foo> actFooAtor = actual.GetEnumerator();
while (expFooAtor.MoveNext())
{
if (actFooAtor.MoveNext())
{
if (!FoosEqual(expFooAtor.Current, actFooAtor.Current)) return false;
}
else
{
MissingFoo(expFooAtor.Current);
return false;
}
}
return true;
}
编辑
由于我犯了一些错误,我不得不稍微修补一下我的示例代码,抱歉。这是原始方法,我改编自以下示例代码:
private static bool CompareXElementsChildXNodes(XElement expectedXElement, XElement actualXElement,
ref string message)
{
_itemLocator.LevelDown();
IEnumerator<XNode> expectedNodeRator = expectedXElement.Nodes().GetEnumerator();
IEnumerator<XNode> actualNodeRator = actualXElement.Nodes().GetEnumerator();
while (expectedNodeRator.MoveNext())
{
if (actualNodeRator.MoveNext())
{
if (CompareXNodes(expectedNodeRator.Current, actualNodeRator.Current, ref message))
{
_itemLocator.NextNode();
}
else
{
return false;
}
}
else
{
ExpectedXNodeActuallyMissing(expectedNodeRator.Current, ref message);
return false;
}
}
_itemLocator.LevelUp();
return true;
}
I was wondering if any one could show me any other ways this method could be written, perhaps using LINQ?
private static bool CompareManyFoos(ManyFoos expected, ManyFoos actual)
{
IEnumerator<Foo> expFooAtor = expected.GetEnumerator();
IEnumerator<Foo> actFooAtor = actual.GetEnumerator();
while (expFooAtor.MoveNext())
{
if (actFooAtor.MoveNext())
{
if (!FoosEqual(expFooAtor.Current, actFooAtor.Current)) return false;
}
else
{
MissingFoo(expFooAtor.Current);
return false;
}
}
return true;
}
EDIT
I've had to patch up my sample code a bit as I made some mistakes, sorry all. This is the original method, I adapted my sample code from:
private static bool CompareXElementsChildXNodes(XElement expectedXElement, XElement actualXElement,
ref string message)
{
_itemLocator.LevelDown();
IEnumerator<XNode> expectedNodeRator = expectedXElement.Nodes().GetEnumerator();
IEnumerator<XNode> actualNodeRator = actualXElement.Nodes().GetEnumerator();
while (expectedNodeRator.MoveNext())
{
if (actualNodeRator.MoveNext())
{
if (CompareXNodes(expectedNodeRator.Current, actualNodeRator.Current, ref message))
{
_itemLocator.NextNode();
}
else
{
return false;
}
}
else
{
ExpectedXNodeActuallyMissing(expectedNodeRator.Current, ref message);
return false;
}
}
_itemLocator.LevelUp();
return true;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许 SequenceEqual 就是您正在寻找的?
Perhaps SequenceEqual would be what you're looking for?