Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.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
C# 如何删除Roles.GetAllRoles()中的一个项_C#_Membership Provider - Fatal编程技术网

C# 如何删除Roles.GetAllRoles()中的一个项

C# 如何删除Roles.GetAllRoles()中的一个项,c#,membership-provider,C#,Membership Provider,我在成员资格提供程序上有一个Roles.GetAllRoles()集合。现在我有一个角色“系统管理员”,我想从列表中删除它,以便在列表中使用。我该怎么做 public void AssignUserToRoles_Activate(object sender, EventArgs e) { try { AvailableRoles.DataSource = Roles.GetAllRoles();

我在成员资格提供程序上有一个Roles.GetAllRoles()集合。现在我有一个角色“系统管理员”,我想从列表中删除它,以便在列表中使用。我该怎么做

public void AssignUserToRoles_Activate(object sender, EventArgs e)
        {
            try
            {
                AvailableRoles.DataSource = Roles.GetAllRoles();
                AvailableRoles.DataBind();
            }
            catch (Exception err)
            {
                //
            }
        }

如果需要,可以使用LINQ将数组转换为列表

var roles = Roles.GetAllRoles().ToList();
roles.Remove("Administrator"); //Yank out the admin role...

AvailableRoles.DataSource = roles;
AvailableRoles.DataBind();
使用
列表

更新:

ToList()
是.Net 3.5捆绑包的一部分。您需要确保您的项目以该框架版本为目标,并且需要确保您的项目具有对System.Core的引用

一旦有了该引用,您需要在代码所在的文件顶部使用语句添加一个

using System.Linq;
如果您拥有所有这些功能,那么您应该开始看到intellisense中出现了一系列新的扩展方法。

角色。GetAllRoles()返回一个字符串数组,您可以使用以下代码对其进行筛选:

        string[] roles = Roles.GetAllRoles();
        var v = from role in roles
                where role != "System Administrator"
                select role;

        AvailableRoles.DataSource = v;
        AvailableRoles.DataBind();

它可以在不向代码中添加任何额外行的情况下实现

public void AssignUserToRoles_Activate(object sender, EventArgs e)
        {
            try
            {
                AvailableRoles.DataSource = Roles.GetAllRoles().Except(new [] {"System Administrator"});
                AvailableRoles.DataBind();
            }
            catch (Exception err)
            {
                //
            }
        }

评论:不确定你为什么需要尝试…抓住这里。但不管怎样,这个解决方案在我看来很不错。

这很奇怪。我没有在Roles.GetAllRoles()之后获取ToList()@dewacorp:数据绑定使用集合接口,包括IEnumerable,而不仅仅是具体的集合类型。也不能这样做:Roles.GetAllRoles()。除了(new[]{“System Administrator”});除此之外没有其他方法?!!?“使用System.Linq”。我认为这是显而易见的。这是“System.Core”dll中的扩展方法。