Reputation: 4104
I've got this chunk of code:
DataTable dt = new DataTable();
dt.Columns.Add("Status");
dt.Columns.Add("File");
dt.Columns.Add("Revision");
int i = 0;
foreach (SvnStatusEventArgs status in statuses) // statuses is a Collection
{
dt.Rows.Add();
switch (status.LocalContentStatus)
{
case SvnStatus.NotVersioned:
dt.Rows[i]["Status"] = "Not Versioned";
break;
default:
dt.Rows[i]["Status"] = status.LocalContentStatus.ToString();
break;
}
dt.Rows[i]["File"] = status.Path;
foreach(SvnInfoEventArgs info in infos) //infos is a Collection
{
if (status.Path.Equals(info.Path))
{
dt.Rows[i]["Revision"] = info.Revision;
break;
}
}
i++;
}
statuses
and infos
can have up to 20K rows each, however, so my nested foreach could take a long time.
I thought I could maybe speed this up if I converted these Collections to Lists and then try to Sort them both by Path
.
Looking over the MSDN Page for the Sort method, I have no idea how I'd go about comparing the Path
field from SvnStatusEventArgs[n] and SvnStatusEventArgs[n+1]. Then I also started to wonder, since I'd be iterating in their entirety over both of these groups of objects and sorting them anyways, would that really be any more efficient than my existing code? I suppose it would be n*2 as opposed to n*n, right?
For what it's worth, the Path
field I'm trying to sort by is just a string.
Upvotes: 0
Views: 97
Reputation: 2123
The best would just be to make a dictionary on infos - with key as path. That would be the most efficient overall.
Upvotes: 1
Reputation: 460108
You could create a Dictionary<string, int>
(the key is the path and the value the revision).
Dictionary<string, int> pathRevisions = infos
.GroupBy(info => info.Path)
.ToDictionary(group => group.Key, group => group.First().Revision);
.... in the loop:
int revision;
if(pathRevisions.TryGetValue(status.Path, out revision))
dt.Rows[i].SetField("Revision", revision);
Upvotes: 5
Reputation: 44439
Your question was fairly unclear but since you said in the comments this is what you meant
foreach (SvnStatusEventArgs status
in statuses
.OrderBy(x => x.Path))
This is a very basic approach though. If you want a more optimal one you should use Tim Schmelter's solution.
Upvotes: 1