Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在一个foreach C中遍历2个形状列表#_C#_Foreach_Office Interop_Office Addins - Fatal编程技术网

C# 在一个foreach C中遍历2个形状列表#

C# 在一个foreach C中遍历2个形状列表#,c#,foreach,office-interop,office-addins,C#,Foreach,Office Interop,Office Addins,我有两个来自两个不同PowerPoint演示文稿的ShapeID列表,一个来自原始PowerPoint,另一个来自编辑过的PowerPoint 现在我想比较这两个列表中的项目。例如,我想比较颜色和字体大小等 我已经尝试了很多方法来实现这一点,并决定最好的方法是遍历2个列表中的每个ShapeId。在一个foreach循环中,我是否可以遍历每个列表?例如foreach(列表1中的Microsoft.Office.Interop.PowerPoint.Slide幻灯片项目1,列表2中的Microsof

我有两个来自两个不同PowerPoint演示文稿的ShapeID列表,一个来自原始PowerPoint,另一个来自编辑过的PowerPoint

现在我想比较这两个列表中的项目。例如,我想比较颜色和字体大小等

我已经尝试了很多方法来实现这一点,并决定最好的方法是遍历2个列表中的每个ShapeId。在一个foreach循环中,我是否可以遍历每个列表?例如
foreach(列表1中的Microsoft.Office.Interop.PowerPoint.Slide幻灯片项目1,列表2中的Microsoft.Office.Interop.PowerPoint.Slide幻灯片项目2)

我的代码是fallows

    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;
在这里,我想浏览这两个列表,并比较每个列表中的每个项目(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();
            }
        }
    }

您可以使用2个字典而不是4个列表,然后使用
Join

以下是一个int和string的示例(将您的问题的string替换为Microsoft.Office.Interop.PowerPoint.Shape):


我希望我的意思很清楚。

因为您希望匹配特定键
Id
上的项目。一个好的选择是使用
连接
。Join将为具有O(1)查找的内部集合构建哈希表


使用它怎么样?:
foreach(editedShapesListID中的Shape editShape){if(originalShapesListID.containts(editShape){int index=originalShapesListID.FindIndex(a=>a.Prop==oProp);Shape originalShp=originalShapesListID.get(index);if(originalShp.RGB!=editShape.RGB){originalShp.Streethrough.ToString()}}
如果您确定两个对象中的项数始终相同,则可以使用“传统”for循环。循环索引可用于引用if语句中两个列表中的对象。@Thangadurai我不认为两个列表的长度相同。你能举一个例子说明for循环的样子吗。Thanks@Thangadurai类似这样的东西?
for(inti=0;i
仅仅因为您将
foreach
扩展到
GetEnumerator/MoveNext
并不意味着您没有循环两次。这仍然是一个问题,只是更难阅读如果我只想扩展一个
foreach
es,它的
MoveNext
将位于另一个foreach之外。您是否尝试模拟
Enum你应该解释一下它的作用,或者只是提到了
Zip
。OP并不是问
Zip
,尽管Zip创建了一个额外的对象(第三个枚举器)在封装同一对枚举器的堆上,委托选择结果和一堆其他字段。我只是觉得这有点过分,因为没有显式使用枚举器。当然,如果任务是通过某个键匹配两个序列的元素,而不是元素的索引,那么就要避免完全匹配搜索或者应该创建一些附加结构,以便在遍历另一个序列时通过键快速查找一个序列的元素,或者应该根据键对序列进行排序。在这种情况下,问题没有正确地提出。
foreach(q中的var项)
似乎不会循环每个形状。知道为什么吗?它会循环所有具有匹配editedShape的形状。如果您想要不匹配的形状,您也必须将其更改为左连接。如果您需要帮助,请告诉我。
    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();
            }
        }
    }
using (var originalEnumerator = originalShapesListID.GetEnumerator())
  foreach (var editShape in editedShapesListID)
  {
    if (!originalEnumerator.MoveNext()) break;
    var original = originalEnumerator.Current;

    ...
  }
Dictionary<int, string> loDicOriginal = new Dictionary<int, string>();
Dictionary<int, string> loDicEdit = new Dictionary<int, string>();

loDicOriginal.Add(1, "int 1 list 1");
loDicOriginal.Add(2, "int 2 list 1");
loDicOriginal.Add(3, "int 3 list 1");

loDicEdit.Add(1, "int 1 list 2");
loDicEdit.Add(3, "int 3 list 2");

var loQuery = loDicOriginal.Join(loDicEdit, 
    dicOrg => dicOrg.Key, 
    dicEdit => dicEdit.Key, 
    (entryOrg, entryEdit) => new { Original = entryOrg, Edit = entryEdit });

foreach (var loOut in loQuery)
{
    System.Diagnostics.Debug.WriteLine("{0}->{1} | {2}->{3}", loOut.Original.Key, loOut.Original.Value, loOut.Edit.Key, loOut.Edit.Value);
}
1->int 1 list 1 | 1->int 1 list 2
3->int 3 list 1 | 3->int 3 list 2
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();
    }
}