1

The following piece of code achives the desired results, but performance is extremely slow:

SearchResultCollection absaUsers = ABSAds.FindAll();
SearchResultCollection srcUsers = ds.FindAll();

foreach (SearchResult users in srcUsers)
{
    string cn = users.Properties["cn"][0].ToString();
    string sn = users.Properties["sn"][0].ToString();
    string userID = users.Properties["uid"][0].ToString();
    string description = users.Properties["PersonnelAreaDesc"][0].ToString();
    string jobCodeID = users.Properties["JobcodeID"][0].ToString();
    string CostCentreID = users.Properties["costCentreID"][0].ToString();
    string CostCentreDescription = users.Properties["CostCentreDescription"][0].ToString();
    string givenName = users.Properties["givenName"][0].ToString();
    string employmentStatus = users.Properties["EmploymentStatus"][0].ToString();
    string EmploymentStatusDescription = users.Properties["EmploymentStatusDescription"][0].ToString();

    foreach (SearchResult absaUser in absaUsers)
    {

        string absaUID = absaUser.Properties["uid"][0].ToString();
        string absaEmploymentStatus = absaUser.Properties["EmploymentStatus"][0].ToString();
        string absaEmploymentStatusDescription = absaUser.Properties["EmploymentStatusDescription"][0].ToString();
        string absaEmployeeNumber = absaUser.Properties["employeeNumber"][0].ToString();

        if (absaUID == cn && absaEmployeeNumber==userID)
        {
            Console.WriteLine("User Record Found:" + cn);
            sw.WriteLine("Modify" + "," + cn + "," + description + "," + userID + "," + givenName + "," + sn + "," + jobCodeID + "," + CostCentreID + "," + CostCentreDescription + "," + sn + "," + cn + "," + employmentStatus + "," + EmploymentStatusDescription);
            sw.Flush();
            break;
        }
    }
}

It loops through 2 collections and mtaches the outer loops attributes with the inner's. Any suggestions on how I can optimise the performance?

3
  • 2
    Any statistics on performance measurements? How many items in the lists absaUsers and srcUsers? Commented Oct 6, 2011 at 9:24
  • If this all come from database, you better do it in the SQL level and Join the tables, fetching only the data you need.. Commented Oct 6, 2011 at 9:26
  • Sorry for not being very specific. This program connects 2 different LDAP Stores, and compares user attributes. absaUsers contain about 50 000 users and scrUsers contain about 11 000 Commented Oct 6, 2011 at 9:29

3 Answers 3

5

It would be faster if you extracted all the absaUID values to a lookup first:

var lookup = absaUsers.Cast<SearchResult>()
                      .ToLookup(x => x.Properties["uid"][0].ToString());

Then you can just use:

foreach (SearchResult users in srcUsers)
{
    string cn = users.Properties["cn"][0].ToString();
    foreach (SearchResult matches in lookup[cn])
    {
        ...
    }
}

You haven't shown how absaUsers is defined - if it's a LINQ query expression, then it could be that your existing code will be going to the database on every iteration at the moment - whereas the above won't. On the other hand, if srcUsers is also a LINQ query expression talking to a database, you should consider doing all the matching at the database using a join instead.

Sign up to request clarification or add additional context in comments.

1 Comment

@Trishen: You'll probably need a call to Cast - will add that.
0

you could use LINQ join, some examples are here, I'm assuming whoever built it into .NET found a pretty optimal way of doing that, and then loop through that. on a sidenote: what are your collection types? please add their declaration to the code snippet.

Comments

0

Use Lamda expressions:

Below is the sample one , You can optizime this to another level.

    List<SearchResult> allResultGroups=new  List<SearchResult>();
    foreach (SearchResult absaUser in absaUsers)
    {
    resultGroups = srcUsers.Where(g => g.cn == absaUser.absaUID && absaUser.absaEmployeeNumber==g.userID ).ToList();
    }

Comments

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.