0

I have the following code:

foreach (XNode rowXml in elements.Nodes())
                         {        
                             foreach (DataRow rowDB in dataSetDB.Tables[0].Rows)
                             {
                                 string itemDB = rowDB[0].ToString().ToUpper();
                                 string itemXml = rowXml.ToString().ToUpper();
                                 if (itemDB == itemXml)
                                 {    
                                    //If itemDB == itemXml; jump to Outer_Foreach
                                 }
                                 if (itemDB != itemXml)
                                 {  
                                     //If itemDB != itemXml; jump to Outer_Foreach
                                 }
                             }

How is it possible to get out of Inner-Foreach and up to Outer-Foreach, and still keep both foreach where you left it. I am looping through a DB and XML table rows. Break; completley jumps out of the Inner-Foreach and im not able to catch where I left it, so I start back on index[0], when I loop through it over and over again.

Philip Kendall
  • 4,304
  • 1
  • 23
  • 42
  • 2
    Strange `if` condition, you jump to the outer-foreach `if(itemDB == itemXml)` and also `if (itemDB != itemXml)`. Do you notice something? So basically you are doing nothing. – Tim Schmelter Sep 04 '13 at 08:26
  • Well, I have code inside those if's – Thomas Wiborg Sep 04 '13 at 08:38
  • You should at least use `else if(itemDB != itemXml)` or even better `else` since both conditions are mutually exclusive. – Tim Schmelter Sep 04 '13 at 08:39
  • Can you explain what in general are you trying to achieve? – Liel Sep 04 '13 at 08:46
  • I have two Tables. One in DB and one in XML file. I check the PK in the first DB row with the PK in the first row of the XML table. If they are similar or different ill do stuff. I then check the second row PK of DB with the second row PK of the XML file. And then it goes on with the rest of the rows. – Thomas Wiborg Sep 04 '13 at 08:49
  • A long shot, but your code looks something like you want the following: http://stackoverflow.com/questions/1955766/iterate-two-lists-or-arrays-with-one-foreach-statement-in-c-sharp ---- edit, you just wrote exactly what I thought. You want the ZIP operation. See the linked stackoverflow question/answer – Daan Timmer Sep 04 '13 at 08:50

2 Answers2

1

It sounds like you need 'for' loop.

 int i = 0;
 int k = 0;
 bool shouldBreak;

 var nodes = elements.Nodes();
 var rows = dataSetDB.Tables[0].Rows;

 for (i = 0; i < nodes.Count(); i++)
 {
     for(k = 0; k < rows.Count(); k++)
     {
        string itemDB = rows[k][0].ToString().ToUpper();
        string itemXml = nodes[i].ToString().ToUpper();
            if (itemDB == itemXml)
            {   
                shouldBreak = true;
                break;
            }
            if (itemDB != itemXml)
            {  
                shouldBreak = true;
                break;
            }
     }
     if (toBreak)
         break;
 }

Now if you'll break the inner loop can know where it broke by accessing i and k

Tomzan
  • 2,808
  • 14
  • 25
0

This answer is stolen from this answer here. What you want to achieve is a zip operation. For more information see the answer that I linked.

var xmlNodes = elements.Nodes();
var dbNodes = dataSetDB.Tables[0].Rows;

var xmlAndDB = xmlNodes.Zip(dbNodes , (x, d) => new { xNode = x, dNode = d });

foreach(var xd in xmlAndDB )
{
    Console.WriteLine(xd.xNode + xd.dNode);

    string itemDB = xd.dNode[0].ToString().ToUpper();
    string itemXml = xd.xNode.ToString().ToUpper();

    if (itemDB == itemXml)
    {    
        //If itemDB == itemXml;
    }
    else /* if (itemDB != itemXml) */ 
    {  
        //If itemDB != itemXml;
    }
}
Community
  • 1
  • 1
Daan Timmer
  • 14,771
  • 6
  • 34
  • 66
  • I get following error mate, "The type arguments for method 'System.Linq.Enumerable.Zip(System.Collections.Generic.I‌​Enumerable, System.Collections.Generic.IEnumerable, System.Func)' cannot be inferred from the usage. Try specifying the type arguments explicitly." – Thomas Wiborg Sep 04 '13 at 10:09