I am having some issues with some Linq again where i am trying to restrict the information coming back but still have the objects in a form of hierarchy (based on the xml its comming from and what is needs to do in the UI) the basic format is this:
- Single Room
- [Rooms Collection]
-
- Room 1 (ID = 1)
-
- Room 2 (ID = 2)
- Twin Room
- [Rooms Collection]
-
- Room 3 (ID = 3)
-
- Room 4 (ID = 4)
but what i want is based on the room id (1,2,3,4 etc) return the room type and the specific room hierachy i.e (if id 4 was passed in).
- Twin Room
- Room 4 (ID = 4)
i created some LINQ which i thought was working, but i was wrong as it simply returns the first Room Type (Single Room) and only a room if the if matches (which i send id 3 and 4 it doesnt) and looking at it i fully understand that.
In the code below items is coming from another method which basically outputs in the example hierachy above:
var item = items.Select(i =>
new RoomType
{
name = i.name,
rooms = i.rooms
.Where(r => r.Name == id.ToString())
}).FirstOrDefault();
What i need to do is return only the room type that has a room in its room collection which has the correct id that i am passing into my method, i am not sure if the code above is a start, i tried messing around with Contains etc but i am searching for an id when Contains is expecting an object so i am not sure what the best approach is here, and i am stuck.
Any help would be great.
Rob
You should filter out room types that don’t contain your room with the particular id (that’s what the
Wherewith theAnycall in it does).