A fast way for matching DataTable and String Array in C#
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 stri开发者_如何学Pythonng 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.
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 DataSet
s 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];
}
}
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.
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.
精彩评论