我一直在关注Pro ASP.net MVC 2框架书,我发现这本书非常出色。但这是一条真正的学习曲线,现在我被困住了。

在这本书中,您将构建类似于以下内容的内容,该内容可以进行分页。

public ViewResult List([DefaultValue(0)] string cityzip, [DefaultValue(1)] int page)
{

var roomsToShow = roomsRepository.Rooms.Where(x => x.CountryID == cityzip);

var viewModel = new RoomsListViewModel
{

    Rooms = roomsToShow.Skip((page - 1) * PageSize).Take(PageSize).ToList(),
    PagingInfo = new PagingInfo
    {
        CurrentPage = page,
        ItemsPerPage = PageSize,
        TotalItems = roomsToShow.Count()
    }
};
return View(viewModel); // Passed to view as ViewData.Model (or simply Model)

}


我认为需要对此进行调整,以便我可以加入搜索

public ViewResult List([DefaultValue(0)] string cityzip, [DefaultValue(1)] int page)
{

var roomsToShow = roomsRepository.Rooms.Join(
                    roomCoordinatesRepository.RoomCoordinates,
                    room => room.RoomID,
                    roomCoordinate => roomCoordinate.RoomID,
                    (room, roomCoordinate) => new { RoomCoordinate = roomCoordinate, Room = room });

var viewModel = new RoomsListViewModel
{

    Rooms = roomsToShow.Skip((page - 1) * PageSize).Take(PageSize).ToList(),
    PagingInfo = new PagingInfo
    {
        CurrentPage = page,
        ItemsPerPage = PageSize,
        TotalItems = roomsToShow.Count()
    }
};
return View(viewModel); // Passed to view as ViewData.Model (or simply Model)

}


...但是我立即收到一个智能错误,说-
无法将类型'System.Collections.Generic.List<AnonymousType#1>'隐式转换为'System.Collections.Generic.IList<MeetingRoom.Domain.Entities.Room>'。存在显式转换(您是否缺少演员表?)

我显然对代码不太了解,无法找出问题所在。我也对这种lamda linq东西感到有些不对劲

Room是一个域对象,定义为:

   namespace MeetingRoom.Domain.Entities
    {
         [Table(Name = "Rooms")]
         public class Room
         {
            [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
            public int RoomID { get; set; }
            [Column] public string Name { get; set; }
            [Column] public string Description { get; set; }
            [Column] public decimal Price { get; set; }
            [Column] public string Category { get; set; }
            [Column] public string Pcode { get; set; }
            [Column] public int CountryID { get; set; }


            public MeetingRooms.Domain.entities.RoomCoordinate RoomCoordinate { get; set; }
      }
    }


并代表我的房间桌子。我需要某种父实体来表示Room和Room-coordinates表之间的联接吗?

坐标实体看起来像这样:

namespace MeetingRooms.Domain.entities
{
    [Table(Name = "RoomCoordinate")]
    public class RoomCoordinate
    {
        [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert, Name = "ID")]
        public int CoordID { get; set; }
        [Column]
        public int RoomID { get; set; }
        [Column]
        public string Coordinates { get; set; }
        [Column]
        public decimal Latitude { get; set; }
        [Column]
        public decimal Longitude { get; set; }
    }
}


RoomsListViewModel如下所示:
命名空间MeetingRoomsMVC.WebUI.Models
{
    公共类RoomsListViewModel
    {
        公共IList RoomsWithCoordinates {get;组; }
        公共PagingInfo PagingInfo {get;组; }
    }
}

最佳答案

这是我根据OP的进一步说明所提出的建议:

1)创建一个同时包含RoomRoomCoordinates信息的聚合类:

public class RoomWithCoordinates
{
    public Room Room { get; set; }
    public RoomCoordinates Coordinates { get; set; }
}


2)修改您的控制器操作,如下所示:

public ViewResult List([DefaultValue(0)] string cityzip, [DefaultValue(1)] int page)
{

var roomsToShow = roomsRepository.Rooms.Join(
                    roomCoordinatesRepository.RoomCoordinates,
                    room => room.RoomID,
                    roomCoordinate => roomCoordinate.RoomID,
                    (room, roomCoordinate) => new RoomWithCoordinates{ Coordinates = roomCoordinate, Room = room } );

var viewModel = new RoomsListViewModel
{

    RoomsWithCoordinates = roomsToShow.Skip((page - 1) * PageSize).Take(PageSize).ToList(),
    PagingInfo = new PagingInfo
    {
        CurrentPage = page,
        ItemsPerPage = PageSize,
        TotalItems = roomsToShow.Count()
    }
};
return View(viewModel); // Passed to view as ViewData.Model (or simply Model)

}


3)修改您的RoomsListViewModel类和视图以反映这些更改。

10-07 23:00