在ForEach语句中使用linq

Ros*_*oss 1 c# linq

我正在尝试使用Linq和我的ForEach语句来显示组中的输出.

我的代码看起来像这样:

Rooms.ToList()
     .ForEach(room => room.RoomContents.ToList()
         .ForEach(roomContents => roomContents.SupportedCommands.ToList()
             .ForEach(command => Console.Write("\nThe commands for {0} are: {1} ", roomContents.Name, command))));          

Console.ReadLine();
Run Code Online (Sandbox Code Playgroud)

电流输出:

The command for Tap are Use
The command for Key are Drop
The command for Key are Get
The command for Key are Use
The command for Bucket are Drop
The command for Bucket are Get
The command for Bucket are Use
Run Code Online (Sandbox Code Playgroud)

我的目标是以更友好的方式显示输出,即根据房间内容对命令进行分组.我希望输出显示这样的东西.

期望的输出:

The commands for Tap 
Use
The commands for Key 
Drop
Get
Use
The commands for Bucket
Drop
Get
Use
Run Code Online (Sandbox Code Playgroud)

Rob*_*Kee 6

Rooms
    .ForEach(room => room.RoomContents.ForEach(roomContents => 
    {
        Console.WriteLine("The commands for {0}",roomContents.Name);
        roomContents.SupportedCommands.ForEach(command => 
           Console.Writeline("{0}",command))
    }));          
Console.ReadLine();
Run Code Online (Sandbox Code Playgroud)

虽然,这不是LINQ的真正好用.我只是自己使用循环.

foreach(var room in Rooms)
{
  foreach(var roomContents in room.RoomContents)
  {
    Console.WriteLine("The commands for {0}",roomContents.Name);
    foreach(var command in roomContents.SupportedCommands)
    {
      Console.Writeline(command);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

第三种可能性是使用Aggregates来构建结果,但同样,它不是很好地使用LINQ.