1

I am trying to wrap my head around LINQ, so I can figure out how to query the DirectoryEntry. At the moment I am trying to write some code in C# that will take a string variable and gave a list of member within a group based upon this string.

Below is what I have managed to figure out so far

public static string[] GetAllUsersInGroup(string groupname)
{
    var names = new List<string>();
    var path = string.Format("WinNT://{0},computer", Environment.MachineName);
    var computerEntry = new DirectoryEntry(path);

    if (computerEntry != null)
    {
        using (computerEntry)
        {
            var menberNames = from DirectoryEntry childEntry
                                in computerEntry.Children.Find("testgroup", "group")
                              where childEntry.SchemaClassName == "User"
                              select childEntry.Name;

            foreach (var name in memberNames)
            {
                names.Add(name);
            }
        }
    }

    return names.ToArray();
}

The problem with this is that I can't use Children.Find() in the where statement.

Although I would like to know how to do this correctly, I really want to be able to figure this out, since there are other queries I need to do. So if anyone know of any GOOD source to find this info please let me know

2
  • 3
    What's the problem here? What isn't working? Commented Sep 26, 2012 at 10:14
  • @PaulAlanTaylor I can't use Children.Find() is the where because it is not a boolean ie if I had something I where childEntry.SchemaClassName == "User" this would work to return all users in the Computer Commented Sep 26, 2012 at 10:20

1 Answer 1

1

I am not really sure about this. Try if it works for you.

public static string[] GetAllUsersInGroup(string groupname)
{
    var path = string.Format("WinNT://{0},computer", Environment.MachineName);

    using (var computerEntry = new DirectoryEntry(path))
    {
        if (computerEntry != null)
        {
            return
                computerEntry.Children.SelectMany(childEntry => 
                    ChildEntry.Children.Find("Administrators", "group")
                        .Children.Select(child => child.Name))
                    .ToArray();
        }
        else 
        {
            return null;
        }
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

You can't do this more LINQy without loosing security
SelectMany is not a property of Children
I have the same issue as @SamStephenson. I cant call any LINQ methods on computerEntry.Children.
@Cody You got using System.Linq; within your document head? :P
@jaydotnet yup. The problem was that Children is an IEnumerable, but not the generic kind, IEnumerable<T>. I had to use Children.OfType<DirectoryEntry>().

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.