I believe your problem is that you don't know which wall belong to your current room. using boundary segment does not guarantee finding correct wall due to 2 reasons:
1. It depends on the whether the walls are set to "RoomBounding" or not.
2. There are other elements can be room bounding such as room separators. Thus, using room boundary can only find the longest BOUNDARY SEMGEMNT, not longest wall.
As for solution, I think you can use a bounding box intersection filter for the given room to find all relevant walls then just find the longest wall among them. You can also query all walls in project and get their Ids for the bounding box intersection filter to improve performance.
This solution has 1 drawback is that you need to tailor your code more to find exact relevant walls for your room in some edgy cases such as:
1. a wall belongs to 2 rooms,
2. 2 walls parallel to each other and each one can belong to a different room, or they can belong to the same room. This can happen when Revit designers try to user multiple parallel walls to represent a compound wall with many layers. Why? They say this trick gives them some advantage when splitting each layer separately and or scheduling the material and volume etc..etc..
Hope this helps.