C#LINQ追加多个项而不是返回它的数组

时间:2014-11-02 08:28:51

标签: 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[].

所以它给了我个性名单。但我想要的是所有角色名称。

是否有LINQ解决方案来解决这个问题?

1 个答案:

答案 0 :(得分:4)

您可以使用SelectMany展平结果:

accounts.SelectMany(a => a.CharacterNames);