用于将匿名类型投影到视图模型的自动映射器查询

Automapper query for projecting an anonymous type onto a viewmodel

我有一个非常基本的控制器,它执行 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);

ViewModel:

 public class ToursAvail2VM
 {
    public int Id { get; set; }
    public int TourCategoryId { get; set; }
    public string TourName { get; set; }
    public int Bookings { get; set; }
 }

如何获得投影到我的 toursvm class 的结果列表?

感谢您的任何建议,马克

您的 linq 查询返回的是匿名类型,而不是 Tour。您需要明确指定要返回的 class,否则 AutoMapper 无法处理它。

但是,由于您使用的是 Linq to Entities,因此无法直接指定 ToursAvail2VM。您可以执行一些操作,例如选择匿名类型,然后立即选择 ToursAvail2VM,如下所示:

IList<ToursAvail2VM> toursvm =
                (from vm in
                    (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()
                select new ToursAvail2VM
                    {
                         Id = vm.Id,
                         TourName = vm.TourName,
                         TourCategoryId = vm.TourCategoryId,
                         Bookings = vm.Bookings
                    }).ToList();

对于这个例子,我将使用 AutoMapper 的 LINQ projection capabilities

// This would be somewhere in initialization
Mapper.CreateMap<Tour, ToursAvail2VM>()
    .ForMember(d => d.Bookings, opt => opt.MapFrom(src => 
        src.TourDates.Sum(td => td.Bookings.Count()));

var toursvm = (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
                )
              .Project().To<ToursAvail2VM>()
              .ToList();

我假设您在此处的模型中包含导航属性,以允许您直接从 Tour 导航到 TourDates 到 Bookings。