在单个foreach C#中迭代2个形状列表
发布时间:2020-12-15 22:41:32 所属栏目:百科 来源:网络整理
导读:我有两个shapeIds列表来自2个单独的PowerPoint演示文稿,一个来自原始PowerPoint,另一个来自编辑过的PowerPoint. 我现在想要将这两个列表中的项目ShapeId相互比较.例如,我想比较颜色和字体大小等. 我已经尝试了很多方法来做到这一点,并决定迭代2个列表中的每
我有两个shapeIds列表来自2个单独的PowerPoint演示文稿,一个来自原始PowerPoint,另一个来自编辑过的PowerPoint.
我现在想要将这两个列表中的项目ShapeId相互比较.例如,我想比较颜色和字体大小等. 我已经尝试了很多方法来做到这一点,并决定迭代2个列表中的每个ShapeId的最佳方法.无论如何我可以在一个foreach循环中迭代每个列表吗?例如foreach(list1中的Microsoft.Office.Interop.PowerPoint.Slide幻灯片item1,list2中的Microsoft.Office.Interop.PowerPoint.Slide幻灯片item2) 我的代码就像休耕一样 Microsoft.Office.Interop.PowerPoint.CustomLayout customLayout = pptPresentationOriginal.SlideMaster.CustomLayouts[Microsoft.Office.Interop.PowerPoint.PpSlideLayout.ppLayoutText]; Microsoft.Office.Interop.PowerPoint.Slides Originalslides; Microsoft.Office.Interop.PowerPoint.Slides EditedSlides; Microsoft.Office.Interop.PowerPoint.Shape originalShp; Microsoft.Office.Interop.PowerPoint.Shape editShp; Originalslides = pptPresentationOriginal.Slides; EditedSlides = pptPresentationEdit.Slides; List<char> l = new List<char>(); List<char> l2 = new List<char>(); List<int> originalShapesListID = new List<int>(); List<int> editedShapesListID = new List<int>(); List<int> originalListID = new List<int>(); List<int> editedListID = new List<int>(); List<Microsoft.Office.Interop.PowerPoint.Shape> originalList = new List<Microsoft.Office.Interop.PowerPoint.Shape>(); List<Microsoft.Office.Interop.PowerPoint.Shape> editList = new List<Microsoft.Office.Interop.PowerPoint.Shape>(); Microsoft.Office.Interop.PowerPoint.Shape editedShpID; 逻辑 String pps = ""; foreach (Microsoft.Office.Interop.PowerPoint.Slide slide in Originalslides) { foreach (Microsoft.Office.Interop.PowerPoint.Shape originalShape in slide.Shapes) { originalShp = originalShape; if (originalShape.HasTextFrame == Microsoft.Office.Core.MsoTriState.msoTrue) { var textFrame = originalShape.TextFrame; if (textFrame.HasText == Microsoft.Office.Core.MsoTriState.msoTrue) { var textRange = textFrame.TextRange; pps += originalShape.TextFrame.TextRange.Text; foreach (char word in pps) { l.Add(word); Debug.WriteLine(word); } } } originalShapesListID.Add(originalShape.Id); originalShapeID = originalShape.Id; originalList.Add(originalShape); } originalListID.Add(slide.SlideID); } foreach (Microsoft.Office.Interop.PowerPoint.Slide slide in EditedSlides) { foreach (Microsoft.Office.Interop.PowerPoint.Shape editShape in slide.Shapes) { editShp = editShape; if (editShape.HasTextFrame == Microsoft.Office.Core.MsoTriState.msoTrue) { var textFrame = editShape.TextFrame; if (textFrame.HasText == Microsoft.Office.Core.MsoTriState.msoTrue) { var textRange = textFrame.TextRange; pps += editShape.TextFrame.TextRange.Text; foreach (char word in pps) { l.Add(word); Debug.WriteLine(word); } } } editedShapesListID.Add(editShape.Id); editedShapeID = editShape.Id; editList.Add(editShape); } editedListID.Add(slide.SlideID); } 这是我想要浏览2个列表并比较每个列表中的每个项目(ShapeId)的地方.我想做这样的事情. foreach (Microsoft.Office.Interop.PowerPoint.Shape editShape in editedShapesListID,Microsoft.Office.Interop.PowerPoint.Shape original in originalShapesListID ) { if (originalShapeID == editedShapeID) { if (original.TextFrame.TextRange.Font.Color.RGB != editShape.TextFrame.TextRange.Font.Color.RGB) { originalShp.TextFrame2.TextRange.Font.StrikeThrough.ToString(); } } } 解决方法
由于您希望匹配特定密钥ID上的项目.一个很好的选择是使用连接. Join将为内部集合构建一个哈希表,该哈希表具有O(1)查找.
var q = from original in originalShapes join editedTmp in editedShapes on original.Id equals editedTmp.Id into g from edited in g.DefaultIfEmpty() select new { original,edited }; foreach(var item in q) { //item.edited might be null if no matching original was found. if (item.edited == null || item.original.TextFrame.TextRange.Font.Color.RGB != item.edited.TextFrame.TextRange.Font.Color.RGB) { item.original.TextFrame2.TextRange.Font.StrikeThrough.ToString(); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |