Take the 2-minute tour ×

I've a list of class type hotel which internally consists list of other class rooms. Now when i perform a descending sort i sort hotels in descending order but i also have to internally sort the rooms list on price using linq. Below is the sketch of the class structure

class Hotel
       - property hotelname (string)
       - property rooms  (list of rooms)

class rooms
      - property roomdesc (string)
      - property roomcharge (decimal)

I've applied the descending and ascending code in below way rite now.

lstHotels.OrderByDescending(Function(hotel) hotel.Rooms(0).roomcharge).ToList()

This sorts the hotel in descending as per the room charge.The 0th index room is always with least cost. Also with the descending sort i also want to sort the list of rooms.

Thanks in advance.

share|improve this question
You didn't state the question clearly. Does your code work or not? Also you talk about reversing a list in your title, but you only talk about ordering a list in your question. –  Meta-Knight Sep 21 '12 at 14:10
Would a C# answer be welcome too? Don't know vb.net enough to express this. –  Wasp Sep 21 '12 at 15:24
Here's what the requirement is ... I need to perform a sorting in descending order on hotels and also the rooms under those hotels.. eg below. Hotel ABC 1st Offer with 2 rooms: 125,150 2nd Offer with 2 rooms : 175,200 Hotel XYZ 1st Offer with 2 rooms: 120,140 2nd Offer with 2 rooms : 210,220 now once i sort these offer in desc Hotel XYZ is displayed as first hotel and also i want to sort the rooms in that offer. I was able to do sort on offer but not on rooms. @Wasp : C# answers are welcomed.. –  VaibhavD Sep 24 '12 at 10:03
add comment

3 Answers

If I understand the question correctly, you want to reverse the order of rooms in a hotel, If so then you can try this.

Dim hotelList as List(Of Hotel) =  lstHotels.OrderByDescending(Function(hotel) hotel.Rooms(0).roomcharge).ToList()

hotelList.ForEach(AddressOf OrderRooms)

Sub OrderRooms(ByVal hotel As Hotel)
End Sub 

Might It will help.

share|improve this answer
add comment

Depending what result you want, you could just need .ThenBy:

Firstly, here is the LINQ expression I'm using:

From hotel In lstHotels
    From room In hotel.Rooms
    Order By hotel.Room(0).roomcharge Descending,
             room.roomcharge Descending    

Using LINQpad I get this:

lstHotels.SelectMany(Function(hotel) hotel.Rooms, _
            Function(hotel,room) New With{.hotel=hotel, .room=room}) _
     .OrderByDescending(Function(hr) hr.hotel.Rooms(0).roomcharge) _
     .ThenByDescending(Function(hr) hr.room.roomcharge)

(but this is untested -- it is based upon the AsQueryable output from a slightly different dataset already coded for another SO answer).

share|improve this answer
add comment

Assuming you have these classes:

public class Hotel
    public string Name { get; set; }
    public List<Room> Rooms { get; set; } // List<Room> to keep it simple, 
                                          // you could/should expose it 
                                          // in a better way

public class Room
    public string Description { get; set; }
    public decimal Charge { get; set; }

I think the right approach would not be to sort the Rooms member, but to do a Linq query that projects the desired sort in new data structures through anonymous types:

var report = from o in offers
             orderby o.Name descending
             select new 
                Rooms = from r in o.Rooms
                        orderby r.Charge
                        select r 

This way you have an enumeration of anonymous types where the first member is the name of the hotel, and the second an enumeration of rooms sorted the way you want. You might want to expose the whole hotel instance replacing o.Name with Hotel = o in your projection, as you like it.

share|improve this answer
add comment

Your Answer


