我在这里需要有关嵌套 LINQ 的帮助。
我的场景是
我有一个列表
HotelId 整型 列表
在房间里面我有一个roomid
我有一个重复的酒店条目和重复的 roomsid 条目
像这样
酒店编号 2500
- 房间号 23
- 房间号 26
酒店编号 3500
- 房间号 50
- 65号房间
酒店编号 2500
- 房间号 23
酒店编号 3900
- 房间号 48
酒店编号 3500
- 32号房间
我想先在酒店找一个团,这当然很简单
listaHoteisEncontrados.GroupBy( x => x.HotelId );
但是我需要一些东西
Dictionary<int,List<rooms>>
其中 int 是我的 IdHotel,rooms 是我在这里尝试过一些示例的房间列表
Dictionary<int, List<Dados.HotelSearchResponse.Room>> otroDic =
listaHoteisEncontrados
.GroupBy( x => x.HotelId )
.ToDictionary(
g => g.Key,
g => g
.GroupBy( e => e.Room
.GroupBy( f => f.RoomType ).ToDictionary
( g2 => g2.Key, g2 => g2.Select( p => p.RoomType ).ToList() )
)
);
没有成功..任何人都可以帮助我吗?
最佳答案
这是另一种可能的解决方案。我只是没能从列表中删除重复的房间。:D
var dic = new Dictionary<int, List<Room>>();
foreach (var hotel in listaHoteisEncontrados)
{
if (!dic.ContainsKey(hotel.Id))
{
dic.Add(hotel.Id, hotel.Rooms);
}
else
{
dic[hotel.Id].AddRange(hotel.Rooms);
//remove duplicate rooms here
}
}
关于c# - 在这种情况下嵌套 LINQ 查询?如何创建嵌套的groupby?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7112132/