Linq lambda加入错误

时间:2011-06-06 10:31:24

标签: c# lambda linq-to-entities

我一直在关注Pro ASP.net MVC 2 Framework这本书,我发现它非常精彩。但这是一个真正的学习曲线,现在我被卡住了。

在本书中,您构建了类似下面的内容,允许分页。

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表。我是否需要某种代表房间和房间坐标表之间连接的父实体?

坐标实体看起来像这样:

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;组; }         public PagingInfo PagingInfo {get;组; }
    } }

2 个答案:

答案 0 :(得分:3)

问题在于此代码

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

您正在构建IEnumerable个匿名类型的对象:(room, roomCoordinate) => new { RoomCoordinate = roomCoordinate, Room = room }

然后,在下一行中,您尝试将其分配到Room列表。

可以通过最初创建正确项目类型的IEnumerable来解决问题:

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

(注意lambda中的类名)。

答案 1 :(得分:1)

以下是基于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课程和您的观点,以反映这些变化。