Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.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# 如何在c中为数组赋值#_C#_Sql_Model View Controller - Fatal编程技术网

C# 如何在c中为数组赋值#

C# 如何在c中为数组赋值#,c#,sql,model-view-controller,C#,Sql,Model View Controller,首先,根据登录用户获取角色名。之后,根据角色从db获取函数代码。我需要将sql结果分配到Array,但它保持显示错误无法从字符串转换为字符串[] public override string[] GetRolesForUser(string username) { DEV_Context OE = new DEV_Context(); string role = OE.UserRefs.Where(x => x.UserName == user

首先,根据登录用户获取角色名。之后,根据角色从db获取函数代码。我需要将
sql
结果分配到
Array
,但它保持显示错误
无法从字符串转换为字符串[]

   public override string[] GetRolesForUser(string username)
    {
        DEV_Context OE = new DEV_Context();
        string role = OE.UserRefs.Where(x => x.UserName == username).FirstOrDefault().RoleName;

        string[] permission = OE.RolePermissionRefs.Where(x => x.RoleName == role).FirstOrDefault().FunctionCode; 
        string[] result={permission};

        return result;
    }

您需要字符串数组,所以不要使用FirstOrDefault,而是使用and-than方法

您的方法应该如下所示

public override string[] GetRolesForUser(string username)
{
    DEV_Context OE = new DEV_Context();
    string role = OE.UserRefs.Where(x => x.UserName == username).FirstOrDefault().RoleName;

    string[] result= OE.RolePermissionRefs.Where(x => x.RoleName == role).Select(x=>x.FunctionCode).ToArray(); 

    return result;
}

你为什么不回许可证呢?此外,我建议您使用集合类型而不是数组。例如,列表