简体   繁体   中英

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.

I now want to compare the items in these 2 lists ShapeId's to each other. 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. Is there anyway I can iterate through each list within one foreach loop? Such as 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. 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 . A good option would be to use a join . Join will build a hash-table for the inner collection which has O(1) lookup.

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 .

Here is an example with int and string (replace string with Microsoft.Office.Interop.PowerPoint.Shape for your issue):

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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