Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/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
C# 将另一个列表中的列表转换为列表<;字符串>;在C中#_C#_Asp.net_Asp.net Core - Fatal编程技术网

C# 将另一个列表中的列表转换为列表<;字符串>;在C中#

C# 将另一个列表中的列表转换为列表<;字符串>;在C中#,c#,asp.net,asp.net-core,C#,Asp.net,Asp.net Core,我在列表中有一个列表,我需要将它转换为字符串列表 下面是列表属性 List<UserOrganization> UserOrganizations { get; set; } public interface UserOrganization { IEnumerable<UserRole> Roles { get; set; } } public class UserRole { public int RoleId

我在列表中有一个列表,我需要将它转换为字符串列表

下面是列表属性

List<UserOrganization> UserOrganizations { get; set; }

public interface UserOrganization
    {
       IEnumerable<UserRole> Roles { get; set; }
    }

public class UserRole
    {
        public int RoleId { get; set; }     
    }
列出用户组织{get;set;}
公共接口用户组织
{
IEnumerable角色{get;set;}
}
公共类用户角色
{
public int RoleId{get;set;}
}
我需要找到所有RoleId并将它们返回到字符串列表中

我尝试了下面的代码,但无法从中获取字符串列表

 var securityRoleIds= CallingUser.UserOrganizations.Select(x => x.Roles.Select(y=> y.RoleId).ToList()).ToList();
List<string> l2 = securityRoleIds.ConvertAll<string>(delegate(Int32 i) { return i.ToString(); });
var securityRoleIds=CallingUser.UserOrganizations.Select(x=>x.Roles.Select(y=>y.RoleId.ToList()).ToList();
List l2=securityRoleIds.ConvertAll(委托(Int32 i){return i.ToString();});
获取此错误

无法将匿名方法转换为“Converter,string>”类型 因为参数类型与委托参数类型不匹配


使用
SelectMany
而不是
Select

List<string> list = CallingUser.UserOrganizations
    .SelectMany(x => x.Roles.Select(y => y.RoleId.ToString()))
    .ToList();
List List=CallingUser.UserOrganizations
.SelectMany(x=>x.Roles.Select(y=>y.RoleId.ToString())
.ToList();
SelectMany
展平嵌套集合


此外,您只需在集合中的每个
int
上使用
ToString()
,而不用在以后转换它们。

使用
选择多个
而不是
选择

List<string> list = CallingUser.UserOrganizations
    .SelectMany(x => x.Roles.Select(y => y.RoleId.ToString()))
    .ToList();
List List=CallingUser.UserOrganizations
.SelectMany(x=>x.Roles.Select(y=>y.RoleId.ToString())
.ToList();
SelectMany
展平嵌套集合

此外,您只需在集合中的每个
int
上使用
ToString()
,而不用在以后转换它们