1

Hello, I need to check if a DirectoryEntry is the last one of a Group in AD. I don't know why, but my boss says he want a Messagebox. The method below is just a little part of my big project. What it does? I gets all users of a group in AD. Every user is saved in a DirectoryEntry. Then I am calling some other stuff. This is irrelevant for this Question see "Do Stuff if de".

I don't know how to get the last element thats why I put this:

var lastelement = users.LastElement;

in the method.

    private void Abgleich()
    {
        log.Debug("Abgleich in ActivDirectory aufgerufen");
        using (var context = new PrincipalContext(ContextType.Domain, Properties.Settings.Default.Servername, Properties.Settings.Default.Container))
        {
            using (var group = GroupPrincipal.FindByIdentity(context, Properties.Settings.Default.ECADGruppe))
            {
                if (group == null)
                {
                    log.Error("Group does not exist");
                }
                else
                {
                    var users = group.GetMembers(true);

                    //Pseudo Code
                    var lastelement = users.LastElement;
                    //End Pseudo Code

                    foreach (UserPrincipal user in users)
                    {
                        DirectoryEntry de = (user.GetUnderlyingObject() as DirectoryEntry);
                        // Do Stuff if de
                        if (de == lastelement)
                        {
                            XtraMessageBox.Show("This is the last", "Remember", MessageBoxButtons.YesNo, MessageBoxIcon.Info);
                        }
                    }
                }
            }
        }
    }
  • Hi Florian, what do you mean with "lastelement"? Is it the newest created entry? If its a specific property of the directory entry, then take a look at the answer to this question: http://stackoverflow.com/questions/2228468/how-to-retrieve-the-creation-date-of-an-ad-user-from-net – Koryu Nov 18 '15 at 13:07
  • For Example in the Group are at the moment of calling 100 users in AD. And one month later there are 110 Users in the AD group. By last I mean the 100th or 110th user. The last user of the group. The last one the foreach is going for. –  Nov 18 '15 at 13:11
  • so you could call the `last()` for `users`. I will post an example – Koryu Nov 18 '15 at 13:22
  • @Koryu users only has Dispose, Equals, GetEnumerator, GetHashcode, GetType and ToString –  Nov 18 '15 at 13:26

1 Answers1

0

There are several ways to find the last object in a collection.

Note: Intellisense won't show all methods if you use var instead of classname in declarations.

One way is to store the last object of the collection in a variable

  PrincipalSearchResult<Principal> users = group.GetMembers(true);


  UserPrincipal lastuser = (UserPrincipal)users.Last();


  foreach (UserPrincipal user in users)
  {
    //...

    if (user == lastuser)
    {
      // Messagebox
    }
  }

Other option is to use a iterate variable

  for (int i = 0; i < users.Count(); i++)
  {       

    if (i == users.Count())
    {
      // .. last user
    }

  }
Koryu
  • 1,371
  • 1
  • 11
  • 21
  • This helps my a lot but I just added you code to my project but it never gets into the If Case –  Nov 18 '15 at 13:41
  • hmm, maybe because I used Principal for lastuser and UserPrincipal for user. I edited it. If thats the problem, choose if you want to use `UserPrincipal` or `Principal` – Koryu Nov 18 '15 at 13:50