简体   繁体   English

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

[英]Iterate through 2 lists of shapes within a single foreach C#

I have 2 lists of shapeIds from 2 seperate PowerPoint presentations, one from and original PowerPoint and another from an edited PowerPoint. 我有两个shapeIds列表来自2个单独的PowerPoint演示文稿,一个来自原始PowerPoint,另一个来自编辑过的PowerPoint。

I now want to compare the items in these 2 lists ShapeId's to each other. 我现在想要将这两个列表中的项目ShapeId相互比较。 For example I want to compare the colour and the font size etc. 例如,我想比较颜色和字体大小等。

I've tried a number of ways to do this and decided the best way to would be to iterate through each ShapeId in the 2 lists. 我已经尝试了很多方法来做到这一点,并决定迭代2个列表中的每个ShapeId的最佳方法。 Is there anyway I can iterate through each list within one foreach loop? 无论如何我可以在一个foreach循环中迭代每个列表吗? Such as foreach (Microsoft.Office.Interop.PowerPoint.Slide slide item1 in list1, Microsoft.Office.Interop.PowerPoint.Slide slide item2 in list2) 例如foreach (Microsoft.Office.Interop.PowerPoint.Slide slide item1 in list1, Microsoft.Office.Interop.PowerPoint.Slide slide item2 in list2)

My code is as 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;

Logic 逻辑

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


    }

Here is where I want to go through the 2 lists and compare each item (ShapeId) in each list. 这是我想要浏览2个列表并比较每个列表中的每个项目(ShapeId)的地方。 I want to do something like this. 我想做这样的事情。

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

Since you want to match the items on a specific key Id . 由于您希望匹配特定密钥Id上的项目。 A good option would be to use a join . 一个很好的选择是使用join Join will build a hash-table for the inner collection which has O(1) lookup. 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();
    }
}
using (var originalEnumerator = originalShapesListID.GetEnumerator())
  foreach (var editShape in editedShapesListID)
  {
    if (!originalEnumerator.MoveNext()) break;
    var original = originalEnumerator.Current;

    ...
  }

You can use 2 Dictionary instead of 4 Lists and then using Join . 您可以使用2个词典而不是4个列表,然后使用“ Join

Here is an example with int and string (replace string with Microsoft.Office.Interop.PowerPoint.Shape for your issue): 下面是一个使用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);
}

Output is: 输出是:

1->int 1 list 1 | 1->int 1 list 2
3->int 3 list 1 | 3->int 3 list 2

I hope it is clear what I mean. 我希望很清楚我的意思。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM