考虑这种情况:
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解决方案来解决这个问题?
答案 0 :(得分:4)
您可以使用SelectMany
展平结果:
accounts.SelectMany(a => a.CharacterNames);