Automapper query for projecting an anonymous type onto a viewmodel

Mark

I have a very basic controller, which does a Linq to Entities query and I want to be able to project the results onto a viewmodel using AutoMapper - however I'm getting the error:

cannot convert from 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.IList<tb.Models.Tour>':

Controller:

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; }
 }

How do I get the list of results projected onto my toursvm class?

Thanks for any advice, Mark

Steve Czetty

Your linq query is returning an anonymous type, rather than Tour. You need to explicitly specify the class you would like returned, or AutoMapper can't handle it.

However, since you are using Linq to Entities, you can't specify ToursAvail2VM directly. You can do something like selecting into the anonymous type and then immediately selecting into ToursAvail2VM like the following:

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();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Project a Query onto an anonymous Dictionary<string,int>

From Dev

Automapper DynamicMap randomly fails to map anonymous type

From Dev

AutoMapper DynamicMap object vs anonymous type

From Dev

projecting points onto a plane given by a normal and a point

From Dev

Projecting a line segment onto a polygon mesh

From Dev

How to bind anonymous type to viewModel in ASP.NET-MVC

From Dev

How to bind anonymous type to viewModel in ASP.NET-MVC

From Dev

Projecting a point onto the intersection of n-dimensional spaces in Python

From Dev

Projecting 3D Model onto 2d plane

From Dev

Summing While Projecting a Dictionary Onto a Sub-Tuple Key

From Dev

Shorten this LINQ query via the help of an anonymous type?

From Dev

Extend an existing (anonymous) type in a select query

From Dev

Extend an existing (anonymous) type in a select query

From Dev

Linq Query return Anonymous Type Error

From Dev

How to convert Anonymous Type to Strong Type from LINQ query

From Dev

LINQ select query with Anonymous type and user Defined type

From Dev

Linq query projecting Id's not names

From Dev

How to create an anonymous type within linq query with TypeScript

From Dev

Convert nullable int to int in linq query return anonymous type

From Dev

Setting all properties of dynamic object in anonymous type in linq query

From Dev

.NET service OData return anonymous type from LINQ query

From Dev

Return Anonymous Type using SqlQuery RAW Query in Entity Framework

From Dev

Using query results with joined parameters without anonymous type

From Dev

Anonymous type result from sql query execution entity framework

From Dev

Setting all properties of dynamic object in anonymous type in linq query

From Dev

Using query results with joined parameters without anonymous type

From Dev

How to make an anonymous type property nullable in the select of a linq to sql query

From Dev

ViewModel replacement for complex Anonymous Object

From Dev

ViewModel replacement for complex Anonymous Object

Related Related

  1. 1

    Project a Query onto an anonymous Dictionary<string,int>

  2. 2

    Automapper DynamicMap randomly fails to map anonymous type

  3. 3

    AutoMapper DynamicMap object vs anonymous type

  4. 4

    projecting points onto a plane given by a normal and a point

  5. 5

    Projecting a line segment onto a polygon mesh

  6. 6

    How to bind anonymous type to viewModel in ASP.NET-MVC

  7. 7

    How to bind anonymous type to viewModel in ASP.NET-MVC

  8. 8

    Projecting a point onto the intersection of n-dimensional spaces in Python

  9. 9

    Projecting 3D Model onto 2d plane

  10. 10

    Summing While Projecting a Dictionary Onto a Sub-Tuple Key

  11. 11

    Shorten this LINQ query via the help of an anonymous type?

  12. 12

    Extend an existing (anonymous) type in a select query

  13. 13

    Extend an existing (anonymous) type in a select query

  14. 14

    Linq Query return Anonymous Type Error

  15. 15

    How to convert Anonymous Type to Strong Type from LINQ query

  16. 16

    LINQ select query with Anonymous type and user Defined type

  17. 17

    Linq query projecting Id's not names

  18. 18

    How to create an anonymous type within linq query with TypeScript

  19. 19

    Convert nullable int to int in linq query return anonymous type

  20. 20

    Setting all properties of dynamic object in anonymous type in linq query

  21. 21

    .NET service OData return anonymous type from LINQ query

  22. 22

    Return Anonymous Type using SqlQuery RAW Query in Entity Framework

  23. 23

    Using query results with joined parameters without anonymous type

  24. 24

    Anonymous type result from sql query execution entity framework

  25. 25

    Setting all properties of dynamic object in anonymous type in linq query

  26. 26

    Using query results with joined parameters without anonymous type

  27. 27

    How to make an anonymous type property nullable in the select of a linq to sql query

  28. 28

    ViewModel replacement for complex Anonymous Object

  29. 29

    ViewModel replacement for complex Anonymous Object

HotTag

Archive