Question Iterate through a list & iterate through a DataTable to produce a result?

ConsKa

Well-known member
Joined
Dec 11, 2020
Messages
140
Programming Experience
Beginner
Firstly, I am not sure this is the best way of doing this.

I have imported data from an excel to create a DataTable. I then have data input that I need to parse out and create a (small L) list of items.

I then want to check whether any of those items appear in the DataTable and if they do, produce an output from a different column.

I created a List<string> for the parsed items, and have been trying to figure out how to loop through the DataTable and loop through the List looking for each item.

I kinda got this far, but I know this is just wrong on so many levels....

C#:
List<string> finalResult = new List<string>();

//code to fill the list

foreach (DataRow row in table.Rows)
{
    int i;
    for (i = 0; i < finalResult.Count; i++)
    {
        if (row.ToString() == finalResult[i].ToString())
        {
            itemsFoundTB.Text += row.ToString();
        }
}

I think it is the IF statement where I can't get my head to wrap around the concept. For instance, I know that row.ToString() just gives me a System.Data response rather than the contents of that row. I know that finalResult[ i ].ToString() doesn't really seem to give me anything.

I just run out of steam, it is 2am and my brain is fried and thought perhaps writing it out here would help - it actually sometimes does, just writing out the question can sometimes help in understanding the problem....but not this time, it didn't help.

So any pointers gratefully received.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,560
Location
Chesapeake, VA
Programming Experience
10+
I know that finalResult[ i ].ToString() doesn't really seem to give me anything
Since finalResult is a List<string>, there is no real point in calling ToString() on a string. Just check against finalResult[i].
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,560
Location
Chesapeake, VA
Programming Experience
10+
I know that row.ToString() just gives me a System.Data response rather than the contents of that row
Correct. That is a fruitless endeavor.

The better approach is to have a triple nested loop. In pseudo code:
C#:
foreach(var row in dataTable.Rows)
{
    foreach(var column in dataTable.Columns)
    {
        var cellValue = row[column].ToString();

       foreach(var result in finalResult)
       {
            if (result == cellValue)
                DoSomethingToMatchingCell(cellValue);
       }
    }
}
 

ConsKa

Well-known member
Joined
Dec 11, 2020
Messages
140
Programming Experience
Beginner
As I laid my weary head upon a soft pillow *cough* it did strike me that I should be iterating through the list? and checking the table...

so, not thought this through yet as just made my morning coffee, but something like:

C#:
foreach (var result in finalResult)
{
    if(table.Row.Contains(result))
    {
        //do something with matching cell
    }
}

Just have to hammer out the the proper language - should read your post fully before posting - you putting me on the right path already...thanks will take a deeper dive on this a bit later.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,071
Location
Sydney, Australia
Programming Experience
10+
As I laid my weary head upon a soft pillow *cough* it did strike me that I should be iterating through the list? and checking the table...

so, not thought this through yet as just made my morning coffee, but something like:

C#:
foreach (var result in finalResult)
{
    if(table.Row.Contains(result))
    {
        //do something with matching cell
    }
}

Just have to hammer out the the proper language - should read your post fully before posting - you putting me on the right path already...thanks will take a deeper dive on this a bit later.
That is fine, in principle. @Skydiver has got the value from the table first and then checked whether it's in the list but you can certainly do it the other way around. There's a lot of logic contained in this one invalid line of code though:
C#:
if(table.Row.Contains(result))
What you could do is write a method that takes a single value and then searches the DataTable for it, e.g.
C#:
private bool IsInTable(string value)
{
    // ...
}
and then your code makes sense if it calls that method:
C#:
foreach (var result in finalResult)
{
    if (IsInTable(result))
    {
        //do something with matching cell
    }
}
Now you just have to actually implement that method but at least you can do that without consideration of your list, so you have simplified the problem. Inside that method, you could loop through the rows of the table and then call a method that searches a single DataRow for a value and you have simplified the problem yet again.

This is how you should approach programming problems: break it down into its constituent parts and address each part separately. If you write a method for each part then you can actually call one of those methods somewhere without having provided an implementation and the calling code will still compile. You can just assume that the method works and defer working on it until later. Each method becomes quite elementary to implement, e.g. IsInTable might look like this:
C#:
private bool IsInTable(string value)
{
    foreach (DataRow row in myDataTable.Rows)
    {
        if (IsInRow(value, row))
        {
            return true;
        }
    }
    
    return false;
}
Simple, right? Now you can independently work on that IsInRow method at your leisure. In the meantime, you could just have it return literal values true and false to test how the rest of the code behaves in those cases.
 
Top Bottom