在单个foreach C#中迭代2个形状列表

时间:2017-01-31 11:27:18

标签: c# foreach office-interop office-addins

我有两个shapeIds列表来自2个单独的PowerPoint演示文稿,一个来自原始PowerPoint,另一个来自编辑过的PowerPoint。

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

我已经尝试了很多方法来做到这一点,并决定了迭代2个列表中每个ShapeId的最佳方法。无论如何我可以在一个foreach循环中迭代每个列表吗?例如foreach (Microsoft.Office.Interop.PowerPoint.Slide slide item1 in list1, Microsoft.Office.Interop.PowerPoint.Slide slide item2 in list2)

我的代码是休闲

    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();
            }
        }
    }

3 个答案:

答案 0 :(得分:1)

由于您希望匹配特定密钥Id上的项目。一个好的选择是使用join。 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();
    }
}

答案 1 :(得分:0)

server {
listen 80;
listen [::]:80;
server_name domain.com;
return 301 $scheme://www.domain.com$request_uri;
##This server block contains no locations etc
}

答案 2 :(得分:0)

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

以下是使用int和string的示例(将问题替换为Microsoft.Office.Interop.PowerPoint.Shape的字符串):

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

我希望我的意思很清楚。