C# 对象的地图集合

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13687240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:26:16  来源:igfitidea点击:

Map collection of objects

c#entity-frameworkautomapper

提问by Riddick

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.

我第一次尝试将 Automapper 引入应用程序,但我一直收到错误消息,说我有一些无效的参数。

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;

在我的控制器中,我试图将选择的商店映射到 storeVM。我目前正在获取我的商店;

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,

我想将商店的选择映射到 StoreVM,我尝试了以下操作,但收到无效参数警告,

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?

任何人都可以指出我正确的方向吗?

采纳答案by Sergey Berezovskiy

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

您正在映射集合,而不是单个实体(IEnumerable<Store>to IEnumerable<StoreVM>),因此请使用此映射

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

回答by Danilo Breda

Update!Now you can do this:

更新!现在你可以这样做:

var VMstores = stores.Project().To<StoreVM>();