0

I am iterating through a DataTable and doing some comparisons. If values match I set a flag to true. I have several cases where the it finds the match, sets the flag (I verify it right after it's set), and in the next bit of code the flag is back to false.

Here is the code that's checking things:

foreach (string code in codeList)
{
    // Filters DataTable by code (should be 6 per code)
    expression = $"[Reason Code] = '{code}'";
    _foundRows = ReasonCodeTable.Select(expression);

    // Goes through the filtered rows
    for (int i = 0; i < _foundRows.Length; i++)
    {
        // Index of Source Row
        int _idx = Convert.ToInt32(_foundRows[i][0]);

        // Desc of Source Row
        string _strDesc = _foundRows[i][5].ToString();

        for (int x = 0; x < _foundRows.Length; x++)
        {
            // Index of the 2nd pass
            int _idx2 = Convert.ToInt32(_foundRows[x][0]);

            // Desc of the 2nd pass row
            string _strDesc2 = _foundRows[x]["Description"].ToString();

            Console.WriteLine($"{code}:  Comparing idx1 {_idx}: '{_strDesc}'  vs  idx2 {_idx2}: '{_strDesc2}'");

            // Loops through filtered list again to compare Descriptions and mark as Found
            if (_idx2 != _idx)
            {
                // Compares Descriptions
                if (_strDesc2.ToString() == _strDesc.ToString())
                {
                    Console.WriteLine($"MATCH FOUND (idx1: {_idx} = idx2: {_idx2})");

                    Console.WriteLine($"idx1 {_idx} Before: {ReasonCodeTable.Rows[_idx]["Found"]}");
                    ReasonCodeTable.Rows[_idx]["Found"] = "True";
                    Console.WriteLine($"idx1 {_idx} After: {ReasonCodeTable.Rows[_idx]["Found"]}");

                    Console.WriteLine($"idx2 {_idx2} After: {ReasonCodeTable.Rows[_idx2]["Found"]}");
                    ReasonCodeTable.Rows[_idx2]["Found"] = "True";
                    Console.WriteLine($"idx2 {_idx2} After: {ReasonCodeTable.Rows[_idx2]["Found"]}");

                    Console.WriteLine($"");
                    x = _foundRows.Length;
                }
            }
        }
    }
}

That gives me the output below. You can see that it's setting all of the flags to true.

4:  Comparing idx1 51: 'Warehouse use and expense'  vs  idx2 51: 'Warehouse use and expense'
4:  Comparing idx1 51: 'Warehouse use and expense'  vs  idx2 52: 'Warehouse use and expense'
MATCH FOUND (idx1: 51 = idx2: 52)
idx1 51 Before: False
idx1 51 After: True
idx2 52 After: False
idx2 52 After: True

4:  Comparing idx1 52: 'Warehouse use and expense'  vs  idx2 51: 'Warehouse use and expense'
MATCH FOUND (idx1: 52 = idx2: 51)
idx1 52 Before: True
idx1 52 After: True
idx2 51 After: True
idx2 51 After: True

4:  Comparing idx1 53: 'Warehouse use and expense'  vs  idx2 51: 'Warehouse use and expense'
MATCH FOUND (idx1: 53 = idx2: 51)
idx1 53 Before: False
idx1 53 After: True
idx2 51 After: True
idx2 51 After: True

7:  Comparing idx1 54: 'Lost Inventory'  vs  idx2 54: 'Lost Inventory'
7:  Comparing idx1 54: 'Lost Inventory'  vs  idx2 55: 'Lost Inventory'
MATCH FOUND (idx1: 54 = idx2: 55)
idx1 54 Before: False
idx1 54 After: True
idx2 55 After: False
idx2 55 After: True

7:  Comparing idx1 55: 'Lost Inventory'  vs  idx2 54: 'Lost Inventory'
MATCH FOUND (idx1: 55 = idx2: 54)
idx1 55 Before: True
idx1 55 After: True
idx2 54 After: True
idx2 54 After: True

7:  Comparing idx1 56: 'Lost Inventory'  vs  idx2 54: 'Lost Inventory'
MATCH FOUND (idx1: 56 = idx2: 54)
idx1 56 Before: False
idx1 56 After: True
idx2 54 After: True
idx2 54 After: True

6:  Comparing idx1 57: 'Customer Samples'  vs  idx2 57: 'Customer Samples'
6:  Comparing idx1 57: 'Customer Samples'  vs  idx2 58: 'Customer Samples'
MATCH FOUND (idx1: 57 = idx2: 58)
idx1 57 Before: False
idx1 57 After: True
idx2 58 After: False
idx2 58 After: True

6:  Comparing idx1 58: 'Customer Samples'  vs  idx2 57: 'Customer Samples'
MATCH FOUND (idx1: 58 = idx2: 57)
idx1 58 Before: True
idx1 58 After: True
idx2 57 After: True
idx2 57 After: True

6:  Comparing idx1 59: 'Customer Samples'  vs  idx2 57: 'Customer Samples'
MATCH FOUND (idx1: 59 = idx2: 57)
idx1 59 Before: False
idx1 59 After: True
idx2 57 After: True
idx2 57 After: True

Right below I have the following bit of code:

// Marks remaining items as either 'Missing' or 'Inconsistent Description'
            foreach (DataRow _dr in ReasonCodeTable.Rows)
            {
                if ((_dr["Issue"].ToString() == "") && (_dr["Found"].ToString() == "False"))
                {
                    Console.WriteLine($"idx: {_dr["ID"]}");
                    Console.WriteLine($"Code: {_dr["Reason Code"]}");
                    Console.WriteLine($"Issue: {_dr["Issue"]}");
                    Console.WriteLine($"Found: {_dr["Found"]}");
                    Console.WriteLine($"");
                    _dr["Issue"] = "Inconsistent Description";
                }
                else if ((_dr["Issue"].ToString() == "Missing"))
                    _dr["Found"] = false;
            }

And I get this output, which shows several of the indices from above that were set true are back to false (specificaly 53, 56, 59)

idx: 27
Code: 20
Issue:
Found: False

idx: 53
Code: 4
Issue:
Found: False

idx: 59
Code: 6
Issue:
Found: False

idx: 56
Code: 7
Issue:
Found: False

idx: 68
Code: GENERAL
Issue:
Found: False

I do not understand why the value of the ["Found"] column is reverting.

2
  • I am not sure how you would expect us to “test” your code. It appears you know “where” the code is switching the value back… have you stepped through that section and examine the values? I am just saying it is not possible for us to reproduce the problem you describe with your currently posted code. Can you edit your question and create a minimal reproducible example so it will be easier to test your code. Commented May 5, 2022 at 4:18
  • The issue is that I was using the ID from the row, which at once point coincided with the row number. However, I found out that it no longer does. I refactored keeping that in mind and I'm good. I'll close. Commented May 5, 2022 at 5:22

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.