views:

325

answers:

3

I have a DataTable with the following columns:

  • id
  • msisdn
  • status
  • another_column

And a string[2][n] array (multi dimensional):

{msisdn, status, useless_data} . . .

I need start from 0 to n in string array and search for the msisdn field in data table. And update the status column in the datatable from the string array's status field.

In datatable msisdn is not unique. More than one column may contain sama msisdn twice or more. However in string array msisdn is unique.

What is the fastest way to match this DataTable and String array and do the stuff above?

Any ideas or help would be appreciated.

+1  A: 

The $100,000 question is whether the data is sorted or can be easily sorted. If one or both of those collections are sorted by msisdn the operation will be much faster: O(n2) for neither sorted, O(n log n) for 1 sorted, O(n) for both.

Of course, the algorithm you use will then vary based on this answer as well, so we'll need to hear a response on that before we can give more detail.

Joel Coehoorn
Given that he states that the data is in a `DataTable`, that would seem to answer all of those questions. However, since the `DataView` maintains a column index on the sorted column, I don't think that the order of the array will have an effect on filter time.
Adam Robinson
+2  A: 

First, I would hope that your string array declaration is more like string[n][2] rather than string[2][n]. For the purposes of this answer I'm going to assume that's the case and it's just a typo.

The fastest way is likely with a DataView, though LINQ to DataSets in .NET 3.5 may be just as good. Something like this:

DataView view = new DataView(yourDataTable);
string[][] data = new string[n][2];

view.Sort = "msisdn";

for(int i = 0; i < theArray; i++)
{
    view.RowFilter = "msisdn = '" + data[i][0] + "'";

    foreach(DataRowView row in view)
    {
        row["status"] = data[i][1];
    }
}
Adam Robinson
+2  A: 

Since the msisdn is unique in the array, you could consider making that array a dictionary keyed by msisdn instead. Iterate through the rows just once and look the key up in the dictionary as you go.

Christian Pena