Map collection of objects

43,192

Solution 1

You are mapping collections, not single entities (IEnumerable<Store> to IEnumerable<StoreVM>), so use this mapping

var VMstores = Mapper.Map<IEnumerable<Store>, IEnumerable<StoreVM>>(stores);

Solution 2

Update! Now you can do this:

var VMstores = stores.Project().To<StoreVM>();
Share:
43,192
Riddick
Author by

Riddick

Systems Development Analyst with a passion for learning, collaborating, sharing and helping!

Updated on July 09, 2022

Comments

  • Riddick
    Riddick almost 2 years

    I am trying to introduce Automapper into an application for the first time, but I keep getting an error saying I have some invalid arguments.

    My model:

    namespace StoreGradesLib.Models
    {
        public class Store
        {
            [Key]
            public int StoreID { get; set; }
    
            [Required]
            [MaxLength(120)]
            public string StoreName { get; set; }
    
            [Required]
            [MaxLength(20)]
            public string StoreNumber { get; set; }
    
            [Required]
            [MaxLength(120)]
            public string ManagerName { get; set; }
    
            [Required]
            public long PhoneNumber { get; set; }
    
            [Required]
            public string AddressLine1 { get; set; }
    
            public string AddressLine2 { get; set; }
    
            [Required]
            public string Postcode { get; set; }
    
            [Required]
            public int WallArea { get; set; }
    
            [Required]
            public int FloorArea { get; set; }
    
            [Required]
            public int NumWindows { get; set; }
    
            [Required]
            public int NumDesks { get; set; }
    
            [Required]
            public int NumDoors { get; set; }
    
            [Required]
            public int StoreGradeID { get; set; }
    
            [Required]
            public bool Active { get; set; }
    
            public virtual StoreGrade StoreGrade { get; set; }
    
            [Timestamp]
            public Byte[] Timestamp { get; set; }
        }
    }
    

    My View Model:

    namespace StoreGradesLib.ViewModels
    {
        public class StoreVM
        {
            public int StoreID { get; set; }
            public bool Active { get; set; }
            public Byte[] Timestamp { get; set; }
    
            [Required(ErrorMessage = "Store Name is required.")]
            [Display(Name = "Store Name")]
            public string StoreName { get; set; }
    
            [Required(ErrorMessage = "Store Number is required")]
            public string StoreNumber { get; set; }
    
            [Required(ErrorMessage = "Store Manager is required.")]
            [Display(Name = "Manager Name")]
            public string ManagerName { get; set; }
    
            [Required(ErrorMessage = "Contact Number is required.")]
            [Display(Name = "Phone Number")]
            public int PhoneNumber { get; set; }
    
            [Required(ErrorMessage = "Address Line 1 is required.")]
            [Display(Name = "Address Line 1")]
            public string AddressLine1 { get; set; }
    
            [Display(Name = "Address Line 2")]
            public string AddressLine2 { get; set; }
    
            [Required(ErrorMessage = "Postcode is required.")]
            public string Postcode { get; set; }
    
            [Required(ErrorMessage = "Must input wall area.")]
            [Display(Name = "Wall Area")]
            public int WallArea { get; set; }
    
            [Required(ErrorMessage = "Must input floor area.")]
            [Display(Name = "Floor Area")]
            public int FloorArea { get; set; }
    
            [Required(ErrorMessage = "Must input number of windows.")]
            [Display(Name = "Windows")]
            public int NumWindows { get; set; }
    
            [Required(ErrorMessage = "Must input number of desks.")]
            [Display(Name = "Desks")]
            public int NumDesks { get; set; }
    
            [Required(ErrorMessage = "Must input number of doors.")]
            [Display(Name = "Doors")]
            public int NumDoors { get; set; }
    
            [Required(ErrorMessage = "Store must have a grade.")]
            public StoreGrade StoreGradeID { get; set; }
    
            public string Address
            {
                get
                {
                    return StoreName + " " + AddressLine1 + " " + AddressLine2 + " " +                 Postcode;
                }
            }
        }
    }
    

    Created mappings:

    CreateMap<Store, StoreVM>();
    CreateMap<StoreVM, Store>();
    

    Within my controller, I am trying to map a selection of stores to storeVM. I am currently getting my stores as so;

    var stores = db.Store.Include(s => s.StoreGrade);
    stores = from s in db.Store.Where(s => s.Active.Equals(true))
                     select s;
    

    I am wanting to map the selection of stores to StoreVM, I have tried the following, but i get an invalid parameters warning,

    var VMstores = Mapper.Map<Store, StoreVM>(stores);
    

    I am receiving the error that the best overloaded method match has some invalid arguments.

    Can anyone point me in the right direction?

  • Riddick
    Riddick over 11 years
    Ah wow thanks! I tried to vote this up but it won't let me, As I use stackoverflow more I will remember to come back and vote you up! Been bugging me for the last half hour, thank you so much!
  • Sergey Berezovskiy
    Sergey Berezovskiy over 11 years
    @user1873098 welcome :) I had same problems when started to use automapper
  • mhichwa
    mhichwa about 10 years
    I voted for this answer but doesn't the documentation say that this should work for Lists, ILists et el? I had the same issues with those types automapper.codeplex.com/wikipage?title=Lists+and+Arrays
  • Moe Howard
    Moe Howard about 8 years
    var VMstores = Mapper.Map<IEnumerable<StoreVM>>(stores); Uses the 1st overload (shorter syntax) rather than the 3rd overload (longer syntax).
  • DanielV
    DanielV over 7 years
    a mapper is required, I suggested it in the edition of the post.
  • Mike Bovenlander
    Mike Bovenlander almost 7 years
    Update: for future reference these are called Queryable Extensions and are included in automapper.