Linq lambda联接错误
本文关键字:错误 lambda Linq | 更新日期: 2023-09-27 17:58:25
我一直在关注ProASP.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)
}
但我立刻得到一个intelligense错误,说-无法将类型'System.Collections.Generic.List<AnonymousType#1>'
隐式转换为'System.Collections.Generic.IList<MeetingRoom.Domain.Entities.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; }
}
}
代表我的房间桌子。我是否需要某种父实体来表示房间和房间坐标表之间的连接?
坐标实体如下所示:
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;set;}公共分页信息分页信息{get;set;}
}}
问题是,在这个代码中,
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中的类名)。
以下是我根据OP的进一步描述提出的建议:
1) 创建一个同时包含Room
和RoomCoordinates
信息的聚合类:
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
类和视图以反映这些更改。