Home > Blockchain >  Error: The type name 'X' does not exist in the type 'Y'
Error: The type name 'X' does not exist in the type 'Y'

Time:12-06

I faced this issue

The type name 'RoomStayVmNew' does not exist in the type 'HotelVmNew.HotelRoomVmNew'

But I my view model,

public class HotelVmNew: IMapFrom<Domain.Entities.Hotel> 
{
    public IEnumerable<HotelRoomVmNew> Rooms { get; set; } = new List<HotelRoomVmNew>();
}

public class HotelRoomVmNew: IMapFrom<Domain.Entities.HotelRoom> 
{
    public IEnumerable<RoomStayVmNew> Stays { get; set; } = new List<RoomStayVmNew>(); 
}

public class RoomStayVmNew: IMapFrom<Domain.Entities.RoomStay> 
{
    public IEnumerable<RoomStayFacilityVm> Facilities { get; set; } = new List<RoomStayFacilityVm>();
}
var hotel = await _context.Hotels
  .Where(i => i.Code == request.Code)
  .Select(i => new HotelVmNew {
    Code = i.Code,
      Rooms = i.Rooms.Select(i => new HotelVmNew.HotelRoomVmNew {
        Id = i.Id,
          Stays = i.Stays.Select(x => new HotelVmNew.HotelRoomVmNew.RoomStayVmNew {}),

      })
  })

When I select HotelRoomVmNew I got the following error:

The type name 'RoomStayVmNew' does not exist in the type 'HotelVmNew.HotelRoomVmNew'.

Why does this error occur?

CodePudding user response:

 new HotelVmNew.HotelRoomVmNew.RoomStayVmNew {}

should be

new RoomStayVmNew {}

And you only need to do that provided you are going to be selecting the room stays as your class definitions are already initializing a new List on construction through the initializer, though I'm guessing though that you've just truncated the initialization for each Stay instance.

This statement:

new HotelVmNew.HotelRoomVmNew.RoomStayVmNew {}

is saying to expect the definition of HotelRoomVmNew to exist within HotelVmNew, and the definition of RoomStayVmNew to exist within the HotelRoomVmNew.

For example:

public class HotelVmNew: IMapFrom<Domain.Entities.Hotel> 
{
    public IEnumerable<HotelRoomVmNew> Rooms { get; set; } = new List<HotelRoomVmNew>();

    public class HotelRoomVmNew: IMapFrom<Domain.Entities.HotelRoom> 
    {
        public IEnumerable<RoomStayVmNew> Stays { get; set; } = new List<RoomStayVmNew>(); 


        public class RoomStayVmNew: IMapFrom<Domain.Entities.RoomStay> 
        {
            public IEnumerable<RoomStayFacilityVm> Facilities { get; set; } = new List<RoomStayFacilityVm>();
        }
    }
}

Note here that the classes are defined within the scope of their parent. You can certainly set this up this way but it would constantly require you to fully expand the class name to get to the dependent class definitions. I cannot think of any good reason to ever do this unless you really want to be clear on ownership of the classes.

  • Related