Reputation: 21
I need a help with nested LINQ here.
My scenario is
I have an List with
HotelId int List
Inside rooms i have a roomid
I have a duplicated entries of hotels and duplicated entries of roomsid
Something like this
HoteId 2500
HotelId 3500
HotelId 2500
HotelId 3900
HotelId 3500
I want a group first by Hotel this is easy of course
listaHoteisEncontrados.GroupBy( x => x.HotelId );
But i need something in
Dictionary<int,List<rooms>>
where int is my IdHotel and rooms is a list of rooms i tried some samples here
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() )
)
);
without sucess.. can anyone help me?
Upvotes: 2
Views: 216
Reputation: 14432
Here's another possible solution. I only didn't manage to remove the duplicate rooms from the list.. :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
}
}
Upvotes: 1