Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用DirectoryEntry LINQ C获取localgroup中的所有用户_Linq_Directoryentry - Fatal编程技术网

使用DirectoryEntry LINQ C获取localgroup中的所有用户

使用DirectoryEntry LINQ C获取localgroup中的所有用户,linq,directoryentry,Linq,Directoryentry,我正试着围绕着LINQ思考,这样我就可以知道如何查询DirectoryEntry了。目前,我正在尝试用C编写一些代码,这些代码将接受一个字符串变量,并根据该字符串给出组中成员的列表 以下是我迄今为止设法弄明白的 public static string[] GetAllUsersInGroup(string groupname) { var names = new List<string>(); var path = string.Format("WinNT://{0

我正试着围绕着LINQ思考,这样我就可以知道如何查询DirectoryEntry了。目前,我正在尝试用C编写一些代码,这些代码将接受一个字符串变量,并根据该字符串给出组中成员的列表

以下是我迄今为止设法弄明白的

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();
}
问题是我不能使用Children。请在where语句中查找


虽然我想知道如何正确地执行此操作,但我真的希望能够解决此问题,因为我还需要执行其他查询。因此,如果有人知道找到此信息的好来源,请让我知道

我对此不是很确定。如果对你有用,试试看

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;
        }
    }
}

我对此不是很确定。如果对你有用,试试看

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;
        }
    }
}

这里有什么问题?什么不起作用?@PaulAlanTaylor我不能使用Children.Find是where,因为它不是一个布尔值ie,如果我有什么东西I where childEntry.SchemaClassName==User这可以返回计算机中的所有用户这里有什么问题?什么不起作用?@PaulAlanTaylor我不能使用Children.Find是where,因为它不是一个布尔值,即如果我有什么东西,I where childEntry.SchemaClassName==User这可以返回计算机中的所有用户如果不失去安全性,你就不能更准确地执行此操作selectmany不是Children的属性我有与@萨姆斯蒂芬森。我不能在computerEntry.Children上调用任何LINQ方法。@Cody您使用的是System.LINQ;在您的文档头中?:P@jaydotnet是的。问题是Children是一个IEnumerable,而不是泛型的IEnumerable。我必须使用Children.OfType.You不能在不失去安全性的情况下更严格地执行此操作selectmany不是Children的财产我与@SamStephenson有相同的问题。我不能在computerEntry.Children上调用任何LINQ方法。@Cody您使用的是System.LINQ;在您的文档头中?:P@jaydotnet是的。问题是Children是一个IEnumerable,而不是泛型的IEnumerable。我不得不使用Children.OfType。