简体   繁体   English

ASP.Net MVC C#AutoMapper使用Where语句

[英]ASP.Net MVC C# AutoMapper using Where statement

using AutoMapper - is it possible, when mapping a domain to a viewmodel, to use the where statement, to limit what is mapped to the viewmodel, eg. 使用AutoMapper - 在将域映射到viewmodel时,可以使用where语句来限制映射到viewmodel的内容,例如。 I use the following to map the Offer list to the OfferVM viewmodel: 我使用以下内容将商品列表映射到OfferVM视图模型:

vm.Offers = Mapper.Map<IList<Offer>, IList<OfferVM>>(offers);

However, I only want to map items in the list Offer to OfferVM if a property on the Offer is set to true, eg: 但是,如果要约中的属性设置为true,我只想将要约列表中的项目映射到OfferVM,例如:

vm.Offers = Mapper.Map<IList<Offer>, IList<OfferVM>>(offers)
           .Where(x => x.RoomName1s==true);

But this gives the error: 但是这给出了错误:

Cannot implicitly convert type
'System.Collections.Generic.IEnumerable<FGBS.ViewModels.OfferVM>'
  to 
'System.Collections.Generic.IList<FGBS.ViewModels.OfferVM>'. 
 An explicit conversion exists (are you missing a cast?)

Thanks for any help. 谢谢你的帮助。

Mark 标记

You need to convert the IEnumerable<OfferVM> returned by Where into IList<OfferVM> using ToList() 您需要使用ToList() Where返回的IEnumerable<OfferVM>转换为IList<OfferVM>

 vm.Offers = Mapper.Map<IList<Offer>, IList<OfferVM>>(offers)
       .Where(x => x.RoomName1s==true)
       .ToList();

您需要添加.ToList()以将IEnumerable转换为List。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM