C#LINQ追加多个项,而不是返回其数组
考虑这种情况:C#LINQ追加多个项,而不是返回其数组,c#,linq,C#,Linq,考虑这种情况: struct Account { public List<string> CharacterNames; } List<Account> accounts = new List<Account>(); // add items to accounts here accounts.Select(a=>a.CharacterNames); // I want it to select string, not string[]. s
struct Account
{
public List<string> CharacterNames;
}
List<Account> accounts = new List<Account>();
// add items to accounts here
accounts.Select(a=>a.CharacterNames); // I want it to select string, not string[].
struct帐户
{
公共列表字符名称;
}
列表帐户=新列表();
//在此处向帐户添加项目
accounts.Select(a=>a.CharacterNames);//我希望它选择字符串,而不是字符串[]。
所以它给了我角色名列表。但我想要的是所有角色的名字
有解决此问题的LINQ解决方案吗?您可以使用
SelectMany
将结果展平:
accounts.SelectMany(a => a.CharacterNames);
您可以使用
SelectMany
将结果展平:
accounts.SelectMany(a => a.CharacterNames);
清楚点!然而,通过谷歌搜索这种问题是非常困难的。感觉清楚!然而,通过谷歌搜索这类问题是非常困难的。