How to get my SortedList<TKey, TValue> C# to compile.

Hi,

I am having problems with the code below. I want to know how I can get it to compile.

The problem is in the last few lines.

            foreach (KeyValuePair<string,AD_OU> ou in OU_List)
            {
                Console.WriteLine("{0} {1}", ou.ObjectGUID, ou.DN);
            }
            Console.WriteLine("");

The completed code is below.

Thanks,

Ward
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;
using System.Collections;

namespace ADSort1
{

    class AD_OU
    {
        public string DN = null;
        public string ObjectGUID = null;
    }

    class Program
    {
        static SortedList<string,AD_OU> OU_List = new SortedList<string,AD_OU>();

        static string fnGet_Object_Category(string starting_path, string filter, string append_condition)
        {
            string category = null;

            DirectoryEntry BaseOU = new DirectoryEntry(starting_path);
            DirectorySearcher searcher = new DirectorySearcher(BaseOU, filter);
            //fnSetSearcher_Settings(ref searcher);
            searcher.PropertiesToLoad.Add("objectCategory");

            SearchResult result = searcher.FindOne();

            category = result.Properties["objectCategory"][0].ToString();
            // "(&(objectCategory=person)(objectClass=user))";
            if (append_condition == "" || append_condition == null)
            {
                category = "(objectCategory=" + category + ")";
            }
            else
            {
                category = "(&(objectCategory=" + category + ")" + append_condition + ")";
            }

            return category;
        }
        // -------------------------------------------------------------------
        // fnSetSearcher_Settings: Set the searcher settings.
        // -------------------------------------------------------------------

        public static void fnSetSearcher_Settings(ref DirectorySearcher searcher)
        {
            searcher.PageSize = 1000;
            searcher.SizeLimit = 10000000;
            searcher.ServerTimeLimit = new TimeSpan(2, 0, 0);
            searcher.ClientTimeout = new TimeSpan(2, 0, 0);
            // searcher.PropertyNamesOnly = true;
        }

        static void Main(string[] args)
        {
            string dn;
            string objectGUID;

            string starting_path = "LDAP://DC=acme,DC=net";
            DirectoryEntry BaseOU = new DirectoryEntry(starting_path);
            string filter = "(objectClass=organizationalUnit)";

            filter = fnGet_Object_Category(starting_path, filter, "");

            DirectorySearcher searcher = new DirectorySearcher(BaseOU, filter);

            fnSetSearcher_Settings(ref searcher);

            searcher.PropertiesToLoad.Add("objectGUID");
            searcher.PropertiesToLoad.Add("distinguishedName");
            SearchResultCollection ad_results = searcher.FindAll();
            foreach (SearchResult result in ad_results)
            {
                dn = result.Properties["distinguishedName"][0].ToString();
                objectGUID = new Guid((byte[])result.Properties["objectGUID"][0]).ToString();
                AD_OU found_ou = new AD_OU();
                found_ou.DN = dn;
                found_ou.ObjectGUID = objectGUID;
                OU_List.Add(objectGUID, found_ou);
            }
            ad_results.Dispose();
            searcher.Dispose();
            Console.WriteLine("");
            foreach (KeyValuePair<string,AD_OU> ou in OU_List)
            {
                Console.WriteLine("{0} {1}", ou.ObjectGUID, ou.DN);
            }
            Console.WriteLine("");
        }
    }
}

Open in new window

LVL 1
whorsfallAsked:
Who is Participating?
 
SriVaddadiConnect With a Mentor Commented:
foreach (KeyValuePair<string, AD_OU> ou in OU_List)
            {
                Console.WriteLine("{0} {1}", ou.Value.ObjectGUID, ou.Value.DN);
            }
0
 
AshokCommented:

            foreach (KeyValuePair<string, AD_OU> ou in OU_List)
            {
                Console.WriteLine("{0} {1}", ou.Key, ou.Value);
            }
            Console.WriteLine("");

HTH
Ashok
0
 
Miguel OzSoftware EngineerCommented:
         foreach (KeyValuePair<string, AD_OU> ou in OU_List)
            {
                Console.WriteLine("{0} {1}", ou.Key, ou.Value.DN);
            }
0
 
Mahmoud_Al_HattabConnect With a Mentor Commented:
Dear whorsfall,

 The best solutiuon is SriVaddadi's one. ashok111 will generate the ou,Value,ToString() to return the type of AD_OU. mas_oz2003's answer works fine as well.

 The explanation why is that the KeyValuePair object in the list holds Key and Value properties; the key holds the string when inserted into it, and the Value is the object itself which is the AD_OU object.

 You could have used this statement as well:
OU_List.Values.ToList().ForEach(ou=>Console.WriteLine("{0} {1}", ou.ObjectGUID, ou.DN));

by this new line using lambda expressions you are accessing the AD_OU objects directly not through the KeyValuePair objects.

See the diffenence with this:
OU_List.ToList().ForEach(kvp => Console.WriteLine("{0} {1}", kvp.Value.ObjectGUID, kvp.Value.DN));

if you still need any further queries please do not hesitate.

regards,
Mahmoud Al-Hattab
0
 
whorsfallAuthor Commented:
Excellent info
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.