【发布时间】:2015-04-21 02:36:27
【问题描述】:
我有一个非常基本的控制器,它执行 Linq to Entities 查询,我希望能够使用 AutoMapper 将结果投影到视图模型上 - 但是我收到了错误:
cannot convert from 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.IList<tb.Models.Tour>':
控制器:
var tours2 = (from t in db.Tours
join d in db.TourDates on t.TourId equals d.TourId
join c in db.TourCategories on t.TourCategoryId equals c.TourCategoryId
where d.Date == dte && t.TourCategoryId == id
select new
{
Id = t.TourId,
TourName = t.TourName,
TourCategoryId = t.TourCategoryId,
Bookings = db.Bookings.Where(b => d.TourDateId == b.TourDateId).Count()
}).ToList();
Mapper.CreateMap<IList<Tour>, IList<ToursAvail2VM>>();
IList<ToursAvail2VM> toursvm = Mapper.Map<IList<Tour>, IList<ToursAvail2VM>>(tours2);
视图模型:
public class ToursAvail2VM
{
public int Id { get; set; }
public int TourCategoryId { get; set; }
public string TourName { get; set; }
public int Bookings { get; set; }
}
如何将结果列表投射到我的 toursvm 类上?
谢谢你的建议,马克
【问题讨论】:
标签: c# linq entity-framework automapper