Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6639107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:30:56+00:00 2026-05-25T23:30:56+00:00

I have written the following program which connects to two LDAP stores, compares the

  • 0

I have written the following program which connects to two LDAP stores, compares the attributes and based on the outcome, creates a new csv file. I have run into a problem though.

Here is the code:

        //Define LDAP Connection
        string username = "****";
        string password = "*****";
        string domain = "LDAP://****";

        //Define LDAP Connection 
        string ABSAusername = "****";
        string ABSApassword = "****";
        string ABSAdomain = "LDAP://****";

        //Create Directory Searcher
        DirectoryEntry ldapConnection = new DirectoryEntry(domain,username,password);
        ldapConnection.AuthenticationType = AuthenticationTypes.Anonymous;
        DirectorySearcher ds = new DirectorySearcher(ldapConnection);
        ds.Filter = "((EmploymentStatus=0))";
        ds.SearchScope = System.DirectoryServices.SearchScope.Subtree;

        //Create Directory Searcher
        DirectoryEntry ABSAldapConnection = new DirectoryEntry(ABSAdomain, ABSAusername, ABSApassword);
        ABSAldapConnection.AuthenticationType = AuthenticationTypes.Anonymous;
        DirectorySearcher ABSAds = new DirectorySearcher(ABSAldapConnection);
        ABSAds.Filter = "((&(EmploymentStatus=3)(EmploymentStatusDescription=Active))";
        ABSAds.SearchScope = System.DirectoryServices.SearchScope.Subtree;

        ds.PropertiesToLoad.Add("cn");
        ds.PropertiesToLoad.Add ("uid");
        ds.PropertiesToLoad.Add("sn");
        ds.PropertiesToLoad.Add("PersonnelAreaDesc");
        ds.PropertiesToLoad.Add("JobcodeID");
        ds.PropertiesToLoad.Add("CostCentreID");
        ds.PropertiesToLoad.Add("CostCentreDescription");
        ds.PropertiesToLoad.Add ("givenName");
        ds.PropertiesToLoad.Add ("EmploymentStatus");
        ds.PropertiesToLoad.Add("EmploymentStatusDescription");

        ABSAds.PropertiesToLoad.Add("uid");
        ABSAds.PropertiesToLoad.Add("EmploymentStatus");

        ABSAds.Sort = new SortOption("uid", SortDirection.Ascending);
        ds.Sort = new SortOption("cn", SortDirection.Ascending);

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

        sw.WriteLine("Action" + "," + "uid" + "," + "Business Area" + "," + "employeeNumber" + "," + "First Name" + "," + "Last Name" + "," + "JobCodeID" + "," + "costCentreID" + "," + "costCentreDescription" + "," + "FullName" + "," + "EmploymentStatus" + "," + "EmploymentStatusDescription" );
        sw.WriteLine("");

        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"][1].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();

                if (cn == absaUID)
                {
                    if (absaEmploymentStatus == "3")
                    {

                        sw.WriteLine(cn);
                    }
                }
            }
        }


        sw.Flush();
        sw.Close();
        sw.Dispose();
    }
}

I created two foreach loops, in the first loop i assigned variables to strings, and in the second foreach loop i do a comparison with an IF statement. What i want to do is: if the uid in one LDAP is equal to the uid in the other ldap, and if the status of the user in the 1st ldap = 0 but the status of the user in the 2nd ldap = 3: then i want to print out the users that match that criteria from the 1st ldap.

If you look through my code, am i doing somthing wrong? The output of the program currently is about 10 users that are duplicated atleast 100 times each.

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:30:56+00:00Added an answer on May 25, 2026 at 11:30 pm

    There are several things obviously wrong with this code….

    1) First of all, you’re creating the same search result twice:

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

    So if that searcher finds 10 users, you have two collections of the same 10 users here.

    2) You then used nested foreach loops, so you basically go through all results from the first collection, one by one, and for each of those entries, you enumerate the entire second collection – basically doing a cartesian product between the two collections. So that’s why you end up with 10 x 10 = 100 users in the end…..

    3) you seem to be missing some kind of a restriction/condition to pick out only those elements from your second/inner result set that match the outer/first result set in some way. As long as you don’t have such a condition in place, you’ll always get all the results from the second result set for each element of the first result set = a classic cartesian product. Somehow, you want to select only certain elements from the second set, based on something from the first result set……

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program written in Delphi-7 which opens a new Word document which
I have the following in a program (written in VB.NET): Imports Microsoft.Office.Interop.Excel Public Class
I have a program written in C, which is named computeWeight.c and to compile
I have written a program which reads input from csv file and its working
I have written a program to insert a row in the contact database which
I have written a java socket server program which listens to a port continuously.
I've written the following program which purpose is to create a file of a
I have written a program which works on huge set of data. My CPU
I have written the following simple test in trying to learn Castle Windsor's Fluent
I have written the following constraint for a column I've called 'grade': CONSTRAINT gradeRule

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.