Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8124937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:38:17+00:00 2026-06-06T06:38:17+00:00

I have the following Models and ViewModels (edited for brevity): public class Advert {

  • 0

I have the following Models and ViewModels (edited for brevity):

public class Advert
{
    public virtual long Id { get; set; }
    public virtual string Title { get; set; }
    public virtual string Descrip { get; set; }
    public virtual IList<AdvertImage> AdvertImages { get; set; }
}

public class AdvertImage
{
    public virtual byte DisplayOrder { get; set; }
    public virtual string Filename { get; set; }
    public virtual string UrlDirectoryRoot { get; set; }
    public virtual long FilesizeBytes { get; set; }

    public virtual Advert Advert { get; set; }
}

public class AdvertImageViewModel
{
    public virtual string Filename { get; set; }
    public virtual byte DisplayOrder { get; set; }
}

public class ListAdvertViewModel
{
    public long Id { get; set; }
    public virtual string Title { get; set; }
    public virtual string Descrip { get; set; }
    public AdvertImageViewModel AdvertImage { get; set; }
}

I have the following AutoMapper mappings:

Mapper.CreateMap<AdvertImage, AdvertImageViewModel>();

Mapper.CreateMap<Advert, ListAdvertViewModel>()
                .ForMember(d => d.AdvertImage, o => o.MapFrom(s => (s.AdvertImages == null ? null : s.AdvertImages.First())));

One of my controller methods then performs the following, to retrieve a list of Adverts (of which each Advert may have either any number of AdvertImages):

var ads = _advertService.GetAdverts();

Then I attempt to map these adverts into my ListAdvertViewModel ViewModel class

var mappedAds = Mapper.Map<IList<Advert>, IList<ListAdvertViewModel>>(ads);

This falls over though if one of the Adverts returned (in the ‘ads’ variable) has some images to map. The error that I get is:

[InvalidOperationException: Sequence contains no elements]
   System.Linq.Enumerable.First(IEnumerable`1 source) +498
   AutoMapper.DelegateBasedResolver`2.Resolve(ResolutionResult source) +153
   System.Linq.Enumerable.Aggregate(IEnumerable`1 source, TAccumulate seed, Func`3 func) +160
   AutoMapper.Mappers.PropertyMapMappingStrategy.MapPropertyValue(ResolutionContext context, IMappingEngineRunner mapper, Object mappedObject, PropertyMap propertyMap) +250

[AutoMapperMappingException: 

Mapping types:
Advert -> AdvertImageViewModel
MyProject.Models.Advert -> MyProject.ViewModels.AdvertImageViewModel

Destination path:
IList`1[1].AdvertImage

I can’t figure out why this AdvertImage mapping is causing problems?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T06:38:19+00:00Added an answer on June 6, 2026 at 6:38 am

    Change First to FirstOrDefault and it should work.

    Mapper.CreateMap<Advert, ListAdvertViewModel>()
                .ForMember(d => d.AdvertImage, o => o.MapFrom(s => (s.AdvertImages == null ? null : s.AdvertImages.FirstOrDefault())));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider I have the following classes as ViewModels public class Address { public String
I have following ViewModel: public class Bulletin1ViewModel { [Required] public String NumberDelegations { get;
I have the following ViewModel: public class AllQuestionsInCategoriesViewModel { public string Category_Name { get;
I have the following view models: public class Search { public int Id {
Let's say I have following models: class Worker { int Id; string firstname; string
I have 2 properties in my ViewModel class ViewModel1 { Dictonary<int, string> PossibleValues {get;set;}//key/value
I have following models setup in my Django application class School(models.Model): name = models.TextField()
Lets say we have following models. class User(db.Model): username=db.StringProperty() avatar=db.ReferenceProperty() class User(db.Model): username=db.StringProperty() avatar=db.StringProperty()
I am using Rails 3.2. I have following models: Blog Comment User class Blog
I have the following models: class Foo has_and_belongs_to_many :bars end class Bar has_and_belongs_to_many :foos

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.