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()
,而不用在以后转换它们